An end-to-end machine learning system to predict whether a loan applicant will default, built with XGBoost, SHAP explainability, customer segmentation, and an interactive Streamlit dashboard.
banking-loan-default-prediction/
├── data/
│ ├── raw/ loan_data.csv (5,000 synthetic records)
│ └── processed/ train.csv, test.csv, final_dataset.csv
├── notebooks/
│ ├── 01_EDA.ipynb
│ ├── 02_Feature_Engineering.ipynb
│ └── 03_Model_Training.ipynb
├── sql/
│ ├── schema.sql Database schema
│ └── business_queries.sql Analytical SQL queries
├── src/
│ ├── data_loader.py
│ ├── preprocessing.py
│ ├── feature_engineering.py
│ ├── eda.py
│ ├── train_model.py
│ ├── evaluate_model.py
│ ├── prediction.py
│ ├── customer_segmentation.py
│ ├── explainability.py
│ └── utils.py
├── models/ Trained artifacts (.pkl)
├── app/
│ └── streamlit_app.py
├── reports/charts/ All EDA + evaluation charts
├── requirements.txt
└── main.py Pipeline orchestrator
pip install -r requirements.txtcd data/raw && python generate_data.pypython main.pyFlags:
python main.py --skip-eda # skip chart generation
python main.py --skip-shap # skip SHAP (faster)
python main.py --threshold 0.4 # custom decision threshold
python main.py --predict-only # score test set with saved modelstreamlit run app/streamlit_app.py| Metric | Value |
|---|---|
| ROC-AUC | ~0.71 |
| F1 Score | ~0.59 |
| Accuracy | ~0.65 |
| Algorithm | XGBoost |
| Imbalance | SMOTE |
| Validation | 5-Fold Stratified CV |
- 34 engineered features including loan-to-income ratio, payment-to-income, risk score composite, credit utilisation proxy, and more
- SMOTE oversampling to handle class imbalance (~44% default rate)
- SHAP global (beeswarm + bar) and local (waterfall) explanations
- K-Means customer segmentation (4 clusters) with PCA visualisation
- Streamlit dashboard with: portfolio overview, single applicant assessment, batch CSV scoring, and model performance charts
- SQL schema + 10 business queries ready for PostgreSQL
| Module | Description |
|---|---|
data_loader.py |
Load & validate raw CSV |
preprocessing.py |
Clean, encode, split |
feature_engineering.py |
14+ derived features, scaling |
eda.py |
7 chart types saved to reports/ |
train_model.py |
XGBoost + SMOTE + 5-fold CV |
evaluate_model.py |
Metrics, ROC, PR, confusion matrix |
prediction.py |
Single & batch inference API |
customer_segmentation.py |
K-Means clustering |
explainability.py |
SHAP global & local explanations |
| Feature | Type | Description |
|---|---|---|
| age | int | Applicant age |
| income | float | Annual income |
| loan_amount | float | Requested loan |
| loan_term | int | Months |
| interest_rate | float | Annual % |
| credit_score | int | 300–850 |
| employment_type | cat | Salaried/Self-Employed/Business/Unemployed |
| loan_purpose | cat | Home/Auto/Education/Personal/Business/Medical |
| loan_grade | cat | A–F |
| debt_to_income | float | DTI ratio |
| num_late_payments | int | Historical late payments |
| previous_defaults | int | Prior defaults |
| … | … | + 8 more raw + 14 engineered |
MIT