This project predicts whether a person will donate blood based on donation history features. The repository includes:
train.pyto train the model and save the artifactsapp.pyto load the saved model and expose a Flask prediction API
Install the required packages before running any script:
pip install pandas numpy matplotlib seaborn scikit-learn imbalanced-learn xgboost scikit-optimize flask flask-cors joblibRun the training script from the project root:
python train.pyThis will:
- load
blood-train.csv - perform preprocessing, feature engineering, SMOTE balancing, and XGBoost tuning
- save the trained model to
blood_donation_xgb_model.joblib - save the scaler to
blood_donation_scaler.joblib
After training completes, run:
python app.pyThe Flask server starts on http://127.0.0.1:5000.
Send a POST request to /predict with JSON input:
curl -X POST http://127.0.0.1:5000/predict ^
-H "Content-Type: application/json" ^
-d "{\"months_last\":10,\"num_donations\":5,\"total_volume\":1250,\"months_first\":36}"Example response:
{
"prediction_text": "Will Donate",
"confidence": 0.85
}| Model | Accuracy | Precision | Recall | F1-Score |
|---|---|---|---|---|
| Logistic Regression | 0.7625 | 0.8234 | 0.5463 | 0.6667 |
| Decision Tree | 0.7891 | 0.8148 | 0.6082 | 0.6982 |
| Random Forest | 0.8061 | 0.8378 | 0.6642 | 0.7417 |
| Support Vector Classifier | 0.7823 | 0.8095 | 0.5864 | 0.6789 |
| XGBoost (Optimized) | 0.8300 | 0.8547 | 0.8696 | 0.8620 |
XGBoost (Optimized) performed best across all four metrics and is the model saved by the training pipeline.
- Run the scripts from the project root so the CSV and model paths resolve correctly.
- If you retrain the model, the
.joblibfiles will be overwritten with the new artifacts.
The application provides an interactive web interface for blood donation predictions. Below are screenshots showcasing the prediction interface:
Input: Months since last donation: 12, Number of donations: 23, Total volume donated: 12500 cc, Months since first donation: 9 Result: Will Not Donate
Input: Months since last donation: 2, Number of donations: 20, Total volume donated: 5000 cc, Months since first donation: 45 Result: Will Donate
Input: Months since last donation: 2, Number of donations: 50, Total volume donated: 12500 cc, Months since first donation: 98 Result: Will Donate