An Interactive Machine Learning Application for Exploring Stroke Classification
Python | Scikit-learn | XGBoost | Streamlit
Stroke Risk Predictor is an interactive web application that demonstrates how machine learning can be integrated into a digital health interface.
Built with Python and Streamlit, the application accepts demographic, health and lifestyle information, processes these inputs using a saved preprocessing object and classification model, and displays a stroke-class prediction alongside the model's estimated probability.
The project brings together machine-learning experimentation, healthcare data analysis, model interpretation and interactive application development.
Project status: Educational machine-learning prototype. The application is not clinically validated and should not be used to assess an individual's medical risk.
- Explore stroke classification using demographic and health-related data.
- Develop and evaluate machine-learning models using a public healthcare dataset.
- Integrate a saved model into an interactive web application.
- Present classification outputs through accessible visualisations.
- Explore global feature importance and predefined health-risk indicators.
- Examine the limitations of predictive models in healthcare applications.
The project uses a publicly available Stroke Prediction Dataset from Kaggle.
The dataset includes demographic, clinical and lifestyle information alongside a recorded stroke outcome used for binary classification.
The application's input features include:
| Category | Variables |
|---|---|
| Demographics | Gender, age, marital history, residence type |
| Health indicators | Hypertension, heart disease, average glucose level, BMI |
| Socioeconomic information | Work type |
| Lifestyle | Smoking status |
The dataset version, sample size, class distribution and detailed preprocessing decisions are documented or explored in the project notebooks.
Stroke-Risk-prediction-App/
βββ app.py
βββ requirements.txt
βββ notebooks/
β βββ eda.ipynb
β βββ preprocessing.ipynb
β βββ modelling.ipynb
βββ models/
βββ stroke_model.pkl
βββ scaler.pkl
The structure above reflects the supplied notebook names and model paths referenced by the application. The saved model files must be present for the application to run.
The eda.ipynb notebook is dedicated to exploring the dataset and understanding the characteristics of the variables used for classification.
The preprocessing.ipynb notebook contains the data preparation work supporting model development.
The deployed application loads a saved preprocessing scaler from models/scaler.pkl and applies it to the input data before prediction.
The modelling.ipynb notebook contains the machine-learning experimentation.
The project's Python dependencies include Scikit-learn, XGBoost and Imbalanced-learn.
The application loads its trained model from models/stroke_model.pkl. The exact final estimator, evaluation metrics and training configuration should be confirmed against the modelling notebook before reporting results.
The trained model and preprocessing object are loaded using Joblib.
When a user submits the form, the application:
- Collects demographic, health and lifestyle inputs.
- Converts categorical responses into numerical features.
- Constructs a feature DataFrame.
- Applies the saved scaler.
- Generates a model probability using
predict_proba(). - Applies a classification threshold of 0.30.
- Displays the classification and supporting visualisations.
The interface organises inputs into three sections:
Demographics: Gender, age, marital history and residential setting.
Health Conditions: Hypertension, heart disease, average glucose level and BMI.
Lifestyle: Employment type and smoking status.
The application displays a High Risk or Low Risk label based on whether the model's output probability reaches the configured 0.30 threshold.
These are application-defined classification labels, not clinically established risk categories.
A percentage display and horizontal risk gauge visualise the model output and indicate the classification threshold.
The displayed probability should not be interpreted as a validated estimate of an individual's future stroke risk.
The application presents a bar chart of the ten highest feature-importance scores reported by the saved model.
These scores describe the model's overall use of features. They do not explain the contribution of each feature to an individual prediction.
The interface also displays rule-based indicators for selected characteristics, including:
- Age of 60 years or above
- Reported hypertension
- Reported heart disease
- Average glucose level above 140
- BMI above 30
- Current or previous smoking
These indicators are derived from predefined application rules and should not be confused with individually calculated feature contributions from the machine-learning model.
| Technology | Purpose |
|---|---|
| Python | Application and modelling language |
| Pandas and NumPy | Data manipulation |
| Scikit-learn | Machine-learning and preprocessing utilities |
| XGBoost | Machine-learning experimentation |
| Imbalanced-learn | Tools for imbalanced classification |
| Joblib | Saving and loading trained objects |
| Streamlit | Interactive application interface |
| Matplotlib | Data visualisation |
| Seaborn | Exploratory visualisation support |
git clone https://github.com/Amara-4312/Stroke-Risk-prediction-App.git
cd Stroke-Risk-prediction-Apppython -m venv .venvActivate it on Windows:
.venv\Scripts\activateOr on macOS/Linux:
source .venv/bin/activatepip install -r requirements.txtstreamlit run app.pyThe application requires the saved model and scaler files referenced in app.py.
The project evaluated four supervised machine-learning algorithms:
- Logistic Regression
- Random Forest
- Gradient Boosting
- XGBoost (
XGBClassifier)
The cleaned dataset contained 5,109 observations, including 249 stroke-positive cases, and 17 predictor columns.
An 80:20 stratified trainβtest split was used to preserve the distribution of the outcome classes.
The training set contained 4,087 observations, while the test set contained 1,022 observations, including 50 stroke-positive cases.
To address class imbalance, SMOTE was applied to the training data only, increasing both training classes to 3,888 observations. The features were subsequently standardised using StandardScaler.
The final deployed model is an XGBoost classifier configured with 200 estimators, maximum tree depth of 4 and learning rate of 0.05. A class-weighting parameter was also used.
The fitted XGBoost model and scaler were saved as Joblib files for integration into the Streamlit application.
Models were evaluated on the held-out test set using classification metrics and ROC-AUC.
| Model | Accuracy | Stroke Precision | Stroke Recall | Stroke F1 | ROC-AUC |
|---|---|---|---|---|---|
| Logistic Regression | 0.93 | 0.35 | 0.42 | 0.38 | 0.820 |
| Random Forest | 0.94 | 0.08 | 0.02 | 0.03 | 0.777 |
| Gradient Boosting | 0.93 | 0.20 | 0.16 | 0.18 | 0.803 |
| XGBoost | 0.74 | 0.12 | 0.68 | 0.20 | 0.789 |
The results demonstrate how model performance depends on the evaluation metric selected.
Random Forest achieved the highest overall accuracy but identified very few stroke-positive cases. Logistic Regression produced the highest ROC-AUC and stroke-class F1-score among the reported experiments.
XGBoost achieved the highest recall for the stroke-positive class, detecting approximately 68% of positive test observations, but with low precision of approximately 12%.
This illustrates the challenge of balancing false negatives and false positives when modelling an infrequent health outcome.
The Streamlit application uses a probability threshold of 0.30 to determine its displayed classification.
The performance metrics above were obtained using the models' default prediction thresholds. They therefore do not represent independently verified performance at the application's deployed threshold.
The results are exploratory rather than externally validated clinical performance estimates.
The XGBoost experiment combined SMOTE with additional positive-class weighting. This may have overemphasised the minority class and should be assessed through further experiments.
Threshold selection, probability calibration and independent validation also remain necessary before any clinical interpretation of model outputs.
Clinical validation: The model has not been validated for clinical diagnosis, screening or individual medical decisions.
Dataset generalisability: A publicly available stroke dataset may not represent patients across different healthcare systems or population groups.
Model probabilities: The interface presents model-generated probabilities, but their calibration and clinical interpretation have not been established.
Classification threshold: The application uses a threshold of 0.30. Its appropriateness requires validation against the intended objective and independent evaluation data.
Feature importance: Global importance scores do not represent individual-level causal explanations.
Risk flags: The displayed flags use predefined application rules rather than a personalised clinical risk-assessment framework.
Preprocessing consistency: The order and definitions of application inputs must match the data and transformations used when training the saved model.
- Verify and document final model evaluation metrics.
- Evaluate probability calibration and classification-threshold selection.
- Assess model performance across demographic subgroups.
- Strengthen reproducibility of the training and preprocessing workflow.
- Explore appropriate methods for individual prediction explanation.
- Improve the presentation of uncertainty and model limitations.
- Conduct external validation before considering any clinical application.
This project demonstrates the process of connecting supervised machine-learning workflows to an interactive application.
It provided practical experience in model integration, input processing, prediction visualisation and communicating the limitations of health-related machine-learning outputs.
The project also illustrates an important distinction in digital health research: building a functional predictive prototype is different from establishing a reliable clinical decision-support system.
Amarachi Emmanuella Nnanyelu
Applied Biochemistry graduate interested in computational biology, health data science, interdisciplinary biomedical research and artificial intelligence.
Medical Disclaimer: This application is intended solely for educational and exploratory research purposes. It is not a diagnostic tool or clinically validated risk calculator. Its outputs should not guide medical decisions.