Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,142 +1,157 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
The dataset is
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
- **
|
| 68 |
-
|
| 69 |
-
###
|
| 70 |
-
|
| 71 |
-
- **
|
| 72 |
-
- **
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
---
|
| 119 |
-
|
| 120 |
-
##
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: StrokeLine - Stroke Prediction Using Machine Learning
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.30.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Stroke Prediction Using Machine Learning
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
## About the Project
|
| 19 |
+
|
| 20 |
+
This project provides a comprehensive machine learning pipeline for predicting the risk of stroke in individuals based on clinical and demographic features. The goal is to enable early identification of high-risk patients, supporting healthcare professionals in making informed decisions and potentially reducing stroke-related morbidity and mortality. The project covers the full data science workflow: data exploration, preprocessing, feature engineering, model selection, hyperparameter optimization, evaluation, explainability, and deployment. The final solution includes a trained model and a Streamlit web application for real-time inference.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## About the Dataset
|
| 25 |
+
|
| 26 |
+
The dataset used is the [Stroke Prediction Dataset](https://www.kaggle.com/datasets/fedesoriano/stroke-prediction-datasett) from Kaggle. It contains 5110 records with 12 features and a binary target variable (`stroke`). The features include:
|
| 27 |
+
|
| 28 |
+
- **id**: Unique identifier (not used for modeling)
|
| 29 |
+
- **gender**: Patient gender (`Male`, `Female`, `Other`)
|
| 30 |
+
- **age**: Age in years
|
| 31 |
+
- **hypertension**: Hypertension status (`0`: No, `1`: Yes)
|
| 32 |
+
- **heart_disease**: Heart disease status (`0`: No, `1`: Yes)
|
| 33 |
+
- **ever_married**: Marital status (`Yes`, `No`)
|
| 34 |
+
- **work_type**: Type of work (`children`, `Govt_job`, `Never_worked`, `Private`, `Self-employed`)
|
| 35 |
+
- **Residence_type**: Living area (`Urban`, `Rural`)
|
| 36 |
+
- **avg_glucose_level**: Average glucose level
|
| 37 |
+
- **bmi**: Body mass index (may contain missing values)
|
| 38 |
+
- **smoking_status**: Smoking behavior (`formerly smoked`, `never smoked`, `smokes`, `Unknown`)
|
| 39 |
+
- **stroke**: Target variable (`1`: Stroke occurred, `0`: No stroke)
|
| 40 |
+
|
| 41 |
+
The dataset is imbalanced, with far fewer positive stroke cases than negatives, and contains missing values in the `bmi` column.
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
|
| 45 |
+
## Notebook Summary
|
| 46 |
+
|
| 47 |
+
The notebook documents the entire process:
|
| 48 |
+
|
| 49 |
+
1. **Problem Definition**: Outlines the clinical motivation, dataset, and challenges.
|
| 50 |
+
2. **EDA**: Visualizes distributions, checks for missing values, and explores feature-target relationships.
|
| 51 |
+
3. **Feature Engineering**: Handles missing data, encodes categorical variables, and examines feature correlations.
|
| 52 |
+
4. **Data Balancing**: Uses RandomUnderSampler and SMOTE to address class imbalance.
|
| 53 |
+
5. **Model Selection**: Compares Random Forest, SVM, and XGBoost classifiers.
|
| 54 |
+
6. **Hyperparameter Tuning**: Uses Optuna for automated optimization of XGBoost.
|
| 55 |
+
7. **Evaluation**: Reports F1 score, confusion matrix, and classification report.
|
| 56 |
+
8. **Explainability**: Applies SHAP for model interpretation.
|
| 57 |
+
9. **Model Export**: Saves the trained model for deployment.
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## Model Results
|
| 62 |
+
|
| 63 |
+
### Preprocessing
|
| 64 |
+
|
| 65 |
+
- **Missing Values**: Imputed missing `bmi` values with the mean.
|
| 66 |
+
- **Categorical Encoding**: Used `OrdinalEncoder` to convert categorical features to numeric.
|
| 67 |
+
- **Feature Selection**: Dropped the `id` column and checked for highly correlated features.
|
| 68 |
+
|
| 69 |
+
### Data Balancing
|
| 70 |
+
|
| 71 |
+
- **RandomUnderSampler**: Reduced the majority class to 10% of its original size.
|
| 72 |
+
- **SMOTE**: Oversampled the minority class to achieve a 1:1 ratio.
|
| 73 |
+
|
| 74 |
+
### Training
|
| 75 |
+
|
| 76 |
+
- **Train-Test Split**: Stratified split to preserve class distribution.
|
| 77 |
+
- **Model Comparison**: Evaluated Random Forest, SVM, and XGBoost on balanced data.
|
| 78 |
+
- **Best Model**: XGBoost achieved the highest F1 score.
|
| 79 |
+
|
| 80 |
+
### Hyperparameter Tuning
|
| 81 |
+
|
| 82 |
+
- **Optuna**: Ran 50 trials to optimize XGBoost hyperparameters (e.g., `n_estimators`, `max_depth`, `learning_rate`, `gamma`, etc.) using 5-fold cross-validation and F1 score as the metric.
|
| 83 |
+
|
| 84 |
+
### Evaluation
|
| 85 |
+
|
| 86 |
+
- **F1 Score**: Achieved ~90% F1 score on the balanced test set.
|
| 87 |
+
- **Confusion Matrix**: Demonstrated balanced sensitivity and specificity.
|
| 88 |
+
- **Classification Report**: Provided detailed precision, recall, and F1 for each class.
|
| 89 |
+
- **Explainability**: SHAP analysis identified the most influential features and provided local/global interpretability.
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
## How to Install
|
| 94 |
+
|
| 95 |
+
Follow these steps to set up the project using a virtual environment:
|
| 96 |
+
|
| 97 |
+
```bash
|
| 98 |
+
# Clone or download the repository
|
| 99 |
+
git clone https://github.com/DeepActionPotential/StrokeLineAI
|
| 100 |
+
cd StrokeLineAI
|
| 101 |
+
|
| 102 |
+
# Create a virtual environment
|
| 103 |
+
python -m venv venv
|
| 104 |
+
|
| 105 |
+
# Activate the virtual environment
|
| 106 |
+
# On Windows:
|
| 107 |
+
venv\Scripts\activate
|
| 108 |
+
# On macOS/Linux:
|
| 109 |
+
source venv/bin/activate
|
| 110 |
+
|
| 111 |
+
# Upgrade pip
|
| 112 |
+
pip install --upgrade pip
|
| 113 |
+
|
| 114 |
+
# Install dependencies
|
| 115 |
+
pip install -r requirements.txt
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
## How to Use the Software
|
| 121 |
+
|
| 122 |
+
1. **Run the Web Application**
|
| 123 |
+
Start the Streamlit app:
|
| 124 |
+
|
| 125 |
+
```bash
|
| 126 |
+
streamlit run app.py
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
2. **Demo**
|
| 130 |
+
## [demo-video](demo/strokeline_demo.mp4)
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
---
|
| 134 |
+
|
| 135 |
+
## Technologies Used
|
| 136 |
+
|
| 137 |
+
### Data Science & Model Training
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
- **matplotlib, seaborn**: Data visualization.
|
| 141 |
+
- **scikit-learn**: Preprocessing, model selection, metrics, and pipelines.
|
| 142 |
+
- **imbalanced-learn**: Advanced resampling (SMOTE, RandomUnderSampler) for class balancing.
|
| 143 |
+
- **XGBoost**: High-performance gradient boosting for classification.
|
| 144 |
+
- **Optuna**: Automated hyperparameter optimization.
|
| 145 |
+
- **SHAP**: Model explainability and feature importance analysis.
|
| 146 |
+
|
| 147 |
+
### Deployment
|
| 148 |
+
|
| 149 |
+
- **Streamlit**: Rapid web app development for interactive model inference.
|
| 150 |
+
- **joblib**: Model serialization for deployment.
|
| 151 |
+
|
| 152 |
+
---
|
| 153 |
+
|
| 154 |
+
## License
|
| 155 |
+
|
| 156 |
+
This project is licensed under the MIT License.
|
| 157 |
+
See the [LICENSE](LICENSE) file for details.
|