A complete end-to-end machine learning project for predicting credit risk and loan defaults. This project demonstrates data preprocessing, feature engineering, model training, evaluation, and deployment via a REST API.
- Overview
- Features
- Project Structure
- Installation
- Quick Start
- Usage
- API Endpoints
- Model Performance
- Configuration
- Testing
- Contributing
- License
This project implements a complete machine learning pipeline for credit risk assessment:
- Data Preprocessing: Handle missing values, scale features using StandardScaler
- Feature Engineering: Create derived features like debt-to-income ratio, credit utilization
- Model Training: Train and compare multiple ML models (Logistic Regression, Decision Tree, Random Forest, Gradient Boosting)
- Model Evaluation: Evaluate with various metrics (Accuracy, Precision, Recall, F1, ROC-AUC)
- Deployment: REST API using Flask for real-time predictions
- Modular architecture with separate components
- Configuration-based settings (YAML)
- Comprehensive logging
- Unit tests
- Makefile for common commands
- Professional package structure
credit-risk-loan-default-prediction/
├── app/
│ └── app.py # Flask REST API
├── config.yaml # Configuration file
├── data/
│ ├── processed/ # Processed data
│ └── raw/ # Raw dataset
├── Makefile # Make commands
├── models/ # Trained models
├── notebooks/
│ └── eda.ipynb # Exploratory Data Analysis
├── README.md
├── requirements.txt
├── setup.py # Package setup
├── src/
│ ├── __init__.py # Package initialization
│ ├── config.py # Configuration loader
│ ├── data_generator.py # Data generation
│ ├── data_preprocessing.py # Data preprocessing
│ ├── feature_engineering.py # Feature engineering
│ ├── logging_config.py # Logging setup
│ ├── model_evaluation.py # Model evaluation
│ ├── model_training.py # Model training
│ ├── train.py # Main training script
│ └── utils.py # Utility functions
└── tests/ # Unit tests
├── __init__.py
└── test_data_preprocessing.py
# Clone the repository
git clone https://github.com/logeshkannan19/Credit-Risk-Loan-Default-Prediction.git
cd Credit-Risk-Loan-Default-Prediction
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Install package in development mode
pip install -e .make installpython -m src.trainOr using Make:
make trainpython -m src.appOr:
make apiThe API will start on http://localhost:5000
curl -X POST http://localhost:5000/predict \
-H "Content-Type: application/json" \
-d '{
"age": 35,
"income": 75000,
"credit_score": 650,
"debt_amount": 15000,
"monthly_expenses": 2500,
"employment_years": 5,
"loan_amount": 25000,
"existing_credits": 2,
"interest_rate": 12.5,
"payment_history": 85
}'The training script can be run with:
python -m src.trainThis will:
- Load and preprocess the data
- Engineer new features
- Train multiple models
- Evaluate and select the best model
- Save the model and preprocessing artifacts
Start the Flask API:
python -m src.app| Endpoint | Method | Description |
|---|---|---|
/health |
GET | Health check |
/predict |
POST | Single prediction |
/batch_predict |
POST | Batch predictions |
/model_info |
GET | Model information |
{
"prediction": "No Default",
"probability": 0.4235,
"risk_level": "Medium"
}- Low: Probability < 0.3
- Medium: 0.3 <= Probability < 0.6
- High: Probability >= 0.6
The model is evaluated using multiple metrics:
| Model | Accuracy | Precision | Recall | F1 Score | ROC-AUC |
|---|---|---|---|---|---|
| Logistic Regression | 0.4947 | 0.6089 | 0.5014 | 0.5500 | 0.4989 |
| Decision Tree | 0.5214 | 0.6126 | 0.6061 | 0.6094 | 0.4946 |
| Random Forest | 0.5506 | 0.6166 | 0.7142 | 0.6618 | 0.5085 |
| Gradient Boosting | 0.5973 | 0.6139 | 0.9320 | 0.7403 | 0.5068 |
Best Model: Random Forest (selected based on ROC-AUC score)
All settings can be configured in config.yaml:
# Training Settings
training:
test_size: 0.2
random_state: 42
cv_folds: 5
# API Settings
api:
host: "0.0.0.0"
port: 5000
# Risk Thresholds
risk:
low: 0.3
medium: 0.6Run tests with:
pytest tests/ -vOr using Make:
make test- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Logesh Kannan - logeshkannan19
- Scikit-learn for machine learning utilities
- Flask for REST API
- The "Give Me Some Credit" dataset inspiration