EUNICE M. Get in Touch
Back to Projects

Machine Learning · Financial AI

Loan Default Risk Predictor

A supervised-learning system that classifies loan applicants as low, medium or high default risk. Two algorithms trained side by side on a Kenyan financial dataset, compared on equal terms, and served through both a Django web interface and an interactive assessor for bank officers.

Python Scikit-learn KNN Naive Bayes Pandas Matplotlib Django OOP

Demo status: this one runs locally rather than on a host — the repository includes the dataset generator, so a fresh clone reproduces the full pipeline and every chart from scratch. Setup steps are at the bottom of this page.

What the model does

Two classifiers, one dataset, and a comparison that decides which ships rather than a hunch.

2 Algorithms KNN vs. Naive Bayes
3 Risk classes Low / Medium / High
200 Applicant records Synthetic Kenyan dataset
4 Diagnostic charts Generated at runtime
K-fold Tuning Cross-validated K selection
OOP Design Single reusable predictor class

The Problem

A loan officer deciding on an application under time pressure is doing informal risk scoring in their head, and two officers looking at the same file can reach different answers. Inconsistency is expensive in both directions: approving defaults costs money, and declining good applicants costs growth.

The goal was a repeatable score grounded in the applicant's actual profile — age, employment status, monthly income, loan amount and term — that an officer can consult without needing to understand the model internals.

My Role

Sole developer. I generated the dataset, built the preprocessing and training pipeline, implemented both classifiers behind one class interface, produced the evaluation visualisations, and wrapped the whole thing in a Django app plus an interactive terminal assessor.

  • Dataset design & generation
  • Feature preprocessing
  • Model training & tuning
  • Evaluation & visualisation
  • Django web layer

How it's put together

Every stage is a script you can run on its own, so the pipeline is reproducible rather than a sequence someone has to remember.

generate_dataset.py

Builds the 200-record synthetic dataset reflecting a Kenyan lending context.

loan_risk_predictor.py

The LoanRiskPredictor class holding preprocessing, training and prediction.

main.py

Runs the full pipeline end to end and writes every output artifact.

interactive_risk_assessor.py

Terminal interface a bank officer can use to score a single applicant.

outputs/

Correlation heatmap, K-tuning curve, confusion matrices and model comparison.

predictor/

Django app with templates, static assets and the model wired into a view.

Engineering decisions

Two algorithms, not one

KNN and Naive Bayes make very different assumptions — distance-based versus probabilistic independence. Training both and comparing them on the same split turns algorithm choice into an evidence question instead of a preference.

Tune K, don't guess it

The neighbour count is selected by cross-validation and the tuning curve is saved as a chart, so the choice is visible and defensible rather than a magic number in the source.

One class, one contract

All the model logic lives in LoanRiskPredictor, so the Django view and the terminal assessor call exactly the same code path. There is no second implementation to drift.

Confusion matrices over accuracy

In credit risk the two error types have very different costs. Per-class confusion matrices show where each model actually fails, which a single accuracy figure hides.

Synthetic but grounded

The generator encodes plausible relationships for a Kenyan lending context rather than random noise, so the model has real structure to learn and the pipeline can be reproduced without exposing anyone's financial data.

Two front doors

A web view for demonstration and a terminal assessor for quick scoring. Same model, two interfaces, no duplicated logic.

Run it locally

A clone reproduces the dataset, the trained models and every chart.

Full pipeline

git clone https://github.com/Eunice-ctrlz/Loan-predictor-model.git
cd Loan-predictor-model
pip install -r requirements.txt
python generate_dataset.py
python main.py

Writes the processed dataset and all four charts into outputs/.

Interfaces

python interactive_risk_assessor.py

python manage.py migrate
python manage.py runserver

The assessor scores one applicant at a time; the Django server exposes the web view on localhost:8000.

Want a walkthrough of the code?

I'm happy to talk through the architecture or the decisions above — and I build systems like this for other people too.