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.
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.
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
-
Clone the repository
git clone https://github.com/<your-username>/CodeAlpha_CreditScoringModel.git cd CodeAlpha_CreditScoringModel
-
Create a virtual environment (recommended)
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Train the model (regenerates everything in
/modelsand/screenshots)python train.py
-
Run a prediction from the command line
python predict.py
-
Launch the Streamlit web app
streamlit run app.py
-
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).
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 |
|---|---|
![]() |
![]() |
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.pyproduces 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.
- 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
- Data Loading β Read the raw CSV dataset into a pandas DataFrame.
- Data Cleaning β Median imputation of missing numeric values.
- Outlier Detection & Treatment β IQR-based winsorization on income, loan amount, and age.
- Exploratory Data Analysis β Class balance, income-by-risk boxplots, credit history breakdown, correlation matrix.
- Feature Engineering β Derived
loan_to_income_ratio,monthly_installment,debt_burden, andcredit_per_dependentβ all established credit-risk signals. - Encoding β Label encoding of categorical variables (employment, credit history, purpose, savings, housing) and the target.
- Feature Scaling β
StandardScalernormalization of all numeric features. - Model Training β Logistic Regression, Decision Tree, Random Forest, Gradient Boosting, and XGBoost trained on an 80/20 stratified train/test split.
- Evaluation β Accuracy, Precision, Recall, F1 Score, ROC-AUC, Confusion Matrix, and full classification report for every model.
- Cross-Validation β 5-fold stratified cross-validation (ROC-AUC) to confirm stability of results across folds.
- Hyperparameter Tuning β
GridSearchCVon the best-performing model family. - Model Persistence β Final tuned model, scaler, encoders, and feature order
saved with
joblibto/models. - Deployment β Streamlit GUI (
app.py) for real-time predictions with a probability score and visual risk meter.
| 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.
- 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.
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.


