Stroke Prediction Project
Overview
This project aims to predict stroke risk using patient data by developing and evaluating multiple machine learning classification models. The best-performing model was analyzed to identify the most significant predictors for the target variable through feature importance.
Dataset
- Source: Kaggle Stroke Prediction Dataset
- Features:
gender, age, hypertension, heart_disease, ever_married, work_type, Residence_type, avg_glucose_level, bmi, smoking_status
- Target Variable:
stroke (Binary: 1 = Stroke, 0 = No Stroke)
- Dataset Size:
- Original: 5110 rows and 12 columns
- After Preprocessing (Normal): 4909 rows and 11 columns (after dropping the
id column and removing missing values)
- After Preprocessing (Oversampling with SMOTE): 9400 rows (balanced dataset with 4700
Stroke and 4700 No Stroke cases)
- Training Data: 7520 rows, 11 columns
- Test Data: 1880 rows, 11 columns
Workflow Pipeline
1. Data Pre-processing and Preparation
1.1 Dropping Irrelevant Columns
- The
id column was removed as it does not contribute to predictive analysis.
1.2 Handling Missing Values
- The
bmi column had 201 missing values (3.9% of the data). These rows were removed due to their small proportion.
1.3 Balancing the Dataset
- The dataset was heavily imbalanced, with a 19:1 ratio between
No Stroke and Stroke.
- SMOTE Oversampling was applied to balance the dataset:
- Before SMOTE:
No Stroke: 4700 samples
Stroke: 209 samples
- After SMOTE:
No Stroke: 4700 samples
Stroke: 4700 samples
2. Exploratory Data Analysis
2.1 Correlation Analysis with Target Variable
- Analyzed the correlation between features and the target variable to identify key predictors.
- Strongest Predictor:
age (0.23): The higher the age, the more likely a stroke is.
- Other Notable Predictors:
hypertension (0.14): Patients with hypertension have an amplified risk of stroke.
avg_glucose_level (0.14): High glucose levels increase the likelihood of a stroke.
heart_disease (0.14): Patients with heart disease face a heightened risk of stroke.
2.2 Feature Correlation
- A heatmap of feature correlations revealed rather obvious correlations, such as:
age and ever_married (-0.68): Younger individuals are less likely to be married.
age and work_type (-0.42): Younger individuals are less likely to be employed.
age and smoking_status (-0.39): Smoking status decreases with younger age.
3. Feature Engineering
3.1 Encoding Categorical Variables
- Categorical features were transformed into numerical values for processing:
gender: Male (0), Female (1), Other (2)
ever_married: Yes (0), No (1)
work_type: Private (0), Self-employed (1), Govt_job (2), Children (3), Never_worked (4)
smoking_status: Formerly smoked (0), Never smoked (1), Smokes (2), Unknown (3)
Residence_type: Urban (0), Rural (1)
3.2 Feature Scaling
- Min-Max scaling was applied to normalize predictors to a range of 0-1, ensuring consistent feature ranges.
4. Model Development and Evaluation
4.1 Data Splitting
-
Before training the models, the dataset was split into training and testing sets:
- Training Set: 7520 samples (80% of the data) used for training the models.
- Testing Set: 1880 samples (20% of the data) held out for evaluating the models' performance on unseen data (see confusion matrices).
4.2 Models Trained
- Five machine learning models suitable for solving classfication tasks were trained and evaluated:
- Random Forest
- Logistic Regression
- K-Nearest Neighbors (KNN)
- Support Vector Machine (SVM)
- Naive Bayes
4.3 Metrics Evaluation
- Each model was evaluated using:
- Accuracy
- Precision
- Recall
- F1-Score
- ROC-AUC
- Runtime (s)
- Confusion Matrices
Results
Training Metrics
The training metrics provide a evaluation of the models' performance on the training data.
For example, the Logistic Regression model achieved:
- Accuracy: 82.8%, meaning 82.8% of training samples were classified correctly.
- Precision: 82.9%, indicating that most predicted strokes were true strokes.
- Recall: 82.8%, meaning the model identified 82.8% of actual stroke cases correctly.
- F1-Score: 82.8%, showing a balanced trade-off between precision and recall.
- ROC-AUC: 91.0%, meaning a strong ability to differentiate between stroke and no-stroke cases.
- Runtime: 0.208 seconds, indicating fast training.
| Model | Accuracy | Precision | Recall | F1-Score | ROC-AUC | Runtime (s) |
|---|
| Random Forest | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 | 0.529 |
| Logistic Regression | 0.828 | 0.829 | 0.828 | 0.828 | 0.910 | 0.208 |
| KNN | 0.876 | 0.885 | 0.876 | 0.875 | 0.960 | 0.005 |
| SVM | 0.800 | 0.811 | 0.800 | 0.798 | 0.874 | 2.681 |
| Naive Bayes | 0.809 | 0.833 | 0.809 | 0.805 | 0.899 | 0.001 |
Testing Metrics (Confusion Matrices):
The confusion matrices provide an overview of how well each model performed on the test dataset by comparing the actual and predicted classes. Each cell represents a percentage of the total cases within each actual class, with values indicating the model's ability to correctly or incorrectly classify cases.
For example, in the case of the Random Forest (after Oversampling) model:
- True Positive (Stroke): It correctly predicted 94.6% of all
Stroke cases as Stroke.
- True Negative (No Stroke): It correctly predicted 88.9% of all
No Stroke cases as No Stroke.
- False Positive (No Stroke predicted as Stroke): 11.1% of
No Stroke cases were incorrectly predicted as Stroke.
- False Negative (Stroke predicted as No Stroke): 5.4% of
Stroke cases were incorrectly predicted as No Stroke.
Confusion Matrices: After vs. Before Oversampling
| Model | After Oversampling | Before Oversampling |
|---|
| Random Forest | TN: 88.9% (836) FP: 11.1% (104) FN: 5.4% (51) TP: 94.6% (889) | TN: 99.5% (935) FP: 0.5% (5) FN: 100% (42) TP: 0.0% (0) |
| Logistic Regression | TN: 82.2% (773) FP: 17.8% (167) FN: 15.0% (141) TP: 85.0% (799) | TN: 100% (940) FP: 0.0% (0) FN: 100% (42) TP: 0.0% (0) |
| KNN | TN: 75.4% (709) FP: 24.6% (231) FN: 10.6% (100) TP: 89.4% (840) | TN: 99.5% (935) FP: 0.5% (5) FN: 2.4% (1) TP: 97.6% (41) |
| SVM | TN: 70.5% (663) FP: 29.5% (277) FN: 11.8% (111) TP: 88.2% (829) | TN: 100% (940) FP: 0.0% (0) FN: 100% (42) TP: 0.0% (0) |
| Naive Bayes | TN: 68.6% (645) FP: 31.4% (295) FN: 6.5% (61) TP: 93.5% (879) | TN: 90.3% (849) FP: 9.7% (91) FN: 64.3% (27) TP: 35.7% (15) |
Model Interpretability
Feature Importance (Random Forest)
The feature importance chart highlights the contribution of each feature to the Random Forest model's predictions.
Feature Importance Scores (Random Forest)
The following are the feature importance scores derived from the Random Forest model, indicating the relative importance of each feature in predicting the likelihood of stroke:
| Feature | Importance |
|---|
| Age | 0.37 |
| Avg Glucose Level | 0.16 |
| BMI | 0.13 |
| Smoking Status | 0.08 |
| Ever Married | 0.08 |
| Work Type | 0.06 |
| Residence Type | 0.05 |
| Gender | 0.03 |
| Hypertension | 0.01 |
| Heart Disease | 0.01 |
Observations and Insights
Best Model: Random Forest
- Achieved perfect training metrics, along with a reasonable runtime of 0.529 seconds.
- The perfect training scores and the slight degradation in test performance indicated overfitting
- However its generalizability was still the best, as it correctly identified 94.6% of
Stroke cases and 88.9% of No Stroke cases on unseen test data (see confusion matrix).
Feature Importance:
- Random Forest identified the following features as most significant for predicting strokes:
age (almost 40% importance): Older individuals are at a higher risk of strokes.
avg_glucose_level (second-most important): Higher glucose levels indicate a greater risk.
bmi (third-most important): High BMI levels also correlate with a higher stroke risk.
Overampling:
- Confusion matrices revealed that most models failed to identify
Stroke cases, with predictions heavily biased toward the majority class No Stroke.
- After oversampling, models showed improved performance in detecting
Stroke cases almost equally well correctly prediciting Stroke and No Stroke on unseen data.