Skip to content

Latest commit

Β 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Credit Score Model Banner

πŸ’³ Credit Scoring Model

CodeAlpha Machine Learning Internship β€” Project

A complete, production-style machine learning system that predicts whether a loan applicant is creditworthy (Good Credit or Bad Credit) using financial, demographic, and loan-related data. The project covers the full ML lifecycle: data cleaning, outlier handling, feature engineering, model comparison, cross-validation, hyperparameter tuning, and deployment through an interactive Streamlit web application.


πŸ“Œ Project Introduction

Credit scoring is one of the most impactful applications of machine learning in the financial industry β€” it determines who gets access to loans and at what risk. This project builds a binary classification system that predicts credit risk based on 12 core applicant features, following the structure of the well-known German Credit (Statlog) dataset.

The project trains and compares five machine learning algorithms, validates them with cross-validation, tunes the best-performing model, and packages it behind a clean, interactive web interface so a loan officer can enter applicant details and instantly get a prediction, a probability score, and a visual risk meter.


πŸ—‚ Project Structure

Credit_Scoring_Model/
β”‚
β”œβ”€β”€ dataset/
β”‚   β”œβ”€β”€ credit_data.csv            # Training dataset (German Credit-style schema)
β”‚   └── generate_dataset.py        # Script used to produce the dataset
β”‚
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ best_model.pkl              # Final tuned model (joblib)
β”‚   β”œβ”€β”€ scaler.pkl                  # Fitted StandardScaler
β”‚   β”œβ”€β”€ encoders.pkl                # Fitted LabelEncoders + target mapping
β”‚   β”œβ”€β”€ feature_columns.pkl         # Ordered feature list used at inference
β”‚   β”œβ”€β”€ best_model_name.txt         # Name of the winning algorithm
β”‚   └── model_comparison.csv        # Metric comparison across all trained models
β”‚
β”œβ”€β”€ notebooks/                      # (reserved for exploratory notebooks)
β”‚
β”œβ”€β”€ screenshots/
β”‚   β”œβ”€β”€ correlation_matrix.png
β”‚   β”œβ”€β”€ class_distribution.png
β”‚   β”œβ”€β”€ income_by_target.png
β”‚   β”œβ”€β”€ feature_importance.png
β”‚   β”œβ”€β”€ confusion_matrix.png
β”‚   β”œβ”€β”€ roc_curve.png
β”‚   β”œβ”€β”€ gui_form.png                # Tkinter desktop app β€” input form
β”‚   └── gui_result.png               # Tkinter desktop app β€” prediction result
β”‚
β”œβ”€β”€ app.py                          # Streamlit GUI application
β”œβ”€β”€ gui_app.py                      # Tkinter desktop GUI application
β”œβ”€β”€ train.py                        # Full training pipeline
β”œβ”€β”€ predict.py                      # CLI prediction script
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ README.md
└── credit_scoring.ipynb            # End-to-end exploratory notebook

βš™οΈ Installation

  1. Clone the repository

    git clone https://github.com/<your-username>/CodeAlpha_CreditScoringModel.git
    cd CodeAlpha_CreditScoringModel
  2. Create a virtual environment (recommended)

    python -m venv venv
    source venv/bin/activate      # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Train the model (regenerates everything in /models and /screenshots)

    python train.py
  5. Run a prediction from the command line

    python predict.py
  6. Launch the Streamlit web app

    streamlit run app.py
  7. Launch the Tkinter desktop app

    python gui_app.py

    On Linux, if you see ModuleNotFoundError: No module named 'tkinter', install it first: sudo apt-get install python3-tk (it ships built-in on Windows and macOS).


πŸ–₯️ Desktop GUI (Tkinter)

In addition to the Streamlit web app, this project now ships a lightweight Tkinter desktop application (gui_app.py) that loads the same trained model artifacts and lets you predict credit risk from a native window β€” no browser or server required.

How it works:

  • Fill in the applicant's details (age, income, employment, loan info, etc.) using text fields and dropdowns.
  • Click "Predict Credit Risk".
  • The app reruns the exact same feature-engineering + scaling + encoding pipeline as predict.py, then displays:
    • Prediction β€” Good Credit / Bad Credit
    • Probability of Bad Credit β€” model confidence
    • Risk Level β€” Low / Moderate / High, color-coded
Input Form Prediction Result
GUI form GUI result

πŸ“Š Dataset

This project uses a dataset built on the German Credit (Statlog) schema, one of the most widely used benchmark datasets for credit-risk research, alongside the feature set specified for this task. It contains 1,000 applicant records with the following 12 input features and 1 binary target:

Feature Description
age Applicant's age in years
income Monthly income
employment Employment duration (unemployed, <1 year, 1-4 years, 4-7 years, >7 years)
loan_amount Requested loan amount
loan_duration Loan repayment duration in months
credit_history Prior credit history (no credits, all paid, existing paid, delayed, critical account)
purpose Purpose of the loan (car, furniture, electronics, education, business, medical, repairs)
savings Savings account balance bracket
housing Housing status (own, rent, free)
installment_rate Installment rate as a percentage of disposable income (1-4)
existing_credits Number of existing credits at this bank
dependents Number of dependents
target Good Credit or Bad Credit

Note: dataset/generate_dataset.py produces this dataset by sampling from distributions and feature relationships calibrated to match the real German Credit dataset's published class balance (~70% good / 30% bad) and risk-factor correlations, so it can be freely regenerated, extended, or swapped for the original UCI/Kaggle CSV without any pipeline changes.


πŸ›  Technologies Used

  • Python 3.11+
  • pandas / NumPy β€” data manipulation
  • scikit-learn β€” preprocessing, modeling, evaluation, cross-validation, tuning
  • XGBoost β€” gradient boosting classifier
  • Matplotlib / Seaborn β€” visualizations
  • Streamlit β€” interactive web application
  • Joblib β€” model & encoder persistence
  • Jupyter Notebook β€” exploratory analysis

πŸ”„ Workflow

  1. Data Loading β€” Read the raw CSV dataset into a pandas DataFrame.
  2. Data Cleaning β€” Median imputation of missing numeric values.
  3. Outlier Detection & Treatment β€” IQR-based winsorization on income, loan amount, and age.
  4. Exploratory Data Analysis β€” Class balance, income-by-risk boxplots, credit history breakdown, correlation matrix.
  5. Feature Engineering β€” Derived loan_to_income_ratio, monthly_installment, debt_burden, and credit_per_dependent β€” all established credit-risk signals.
  6. Encoding β€” Label encoding of categorical variables (employment, credit history, purpose, savings, housing) and the target.
  7. Feature Scaling β€” StandardScaler normalization of all numeric features.
  8. Model Training β€” Logistic Regression, Decision Tree, Random Forest, Gradient Boosting, and XGBoost trained on an 80/20 stratified train/test split.
  9. Evaluation β€” Accuracy, Precision, Recall, F1 Score, ROC-AUC, Confusion Matrix, and full classification report for every model.
  10. Cross-Validation β€” 5-fold stratified cross-validation (ROC-AUC) to confirm stability of results across folds.
  11. Hyperparameter Tuning β€” GridSearchCV on the best-performing model family.
  12. Model Persistence β€” Final tuned model, scaler, encoders, and feature order saved with joblib to /models.
  13. Deployment β€” Streamlit GUI (app.py) for real-time predictions with a probability score and visual risk meter.

πŸ“ˆ Results

Model Accuracy Precision Recall F1 Score ROC-AUC
Gradient Boosting 0.850 0.813 0.650 0.722 0.905
Random Forest 0.785 0.681 0.533 0.598 0.862
XGBoost 0.800 0.692 0.600 0.643 0.852
Logistic Regression 0.805 0.723 0.567 0.636 0.828
Decision Tree 0.795 0.694 0.567 0.624 0.791

Gradient Boosting was selected as the best model and further improved with hyperparameter tuning (learning_rate=0.05, max_depth=3, n_estimators=200), achieving a cross-validated ROC-AUC of 0.906 on the held-out test set. The most predictive features identified were credit history, savings balance, employment duration, and the engineered debt-burden and loan-to-income ratios β€” consistent with established credit risk assessment practices.

All charts (correlation matrix, feature importance, confusion matrix, ROC curve, and class distribution) are available in /screenshots and are also rendered live inside the Streamlit app's Model Insights tab.


πŸš€ Future Work

  • Train on the original German Credit / Give Me Some Credit dataset for stronger real-world validity and regulatory-grade validation.
  • Address class imbalance more explicitly with SMOTE, ADASYN, or class-weighting.
  • Add SHAP-based explainability so loan officers can see why an application was flagged as high risk (important for lending transparency and compliance).
  • Experiment with stacking/ensembling the top three models.
  • Deploy the model as a REST API (FastAPI) alongside the Streamlit front end.
  • Add model monitoring for concept drift as applicant behavior changes over time.
  • Containerize the app with Docker for one-command deployment.

βš•οΈ Disclaimer

This project is developed strictly for educational purposes as part of the CodeAlpha Machine Learning Internship. It is not intended for real lending or credit decisions and should never be used as a substitute for a certified credit risk assessment process.


πŸ‘¨β€πŸ’» Author

Irtaza Hyder Machine Learning Intern at CodeAlpha Bachelor of Science in Computer Science (BSCS)

⭐ If you found this project useful, consider giving the repository a star.

About

πŸ’³ ML system predicting customer creditworthiness (Good/Bad Credit) using Logistic Regression, Random Forest, Gradient Boosting & XGBoost, with a Streamlit risk-scoring app. Built for the CodeAlpha ML Internship.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages