← Back to Projects

Credit Risk Intelligence Platform

A loan-default scoring platform on 58.5 million rows of lending data, with a calibrated LightGBM model, cost-based approval thresholds, SHAP explanations, and a Gemini chat that writes SQL I check before it runs.

Links GitHub
Technologies
Python LightGBM scikit-learn SHAP DuckDB SQL Gemini API FastAPI Docker

What it is

This scores how likely a loan applicant is to default. It’s built on the Home Credit Default Risk dataset, which has 307,511 labelled applications spread across 58.5 million rows in eight tables.

I load the raw CSVs into DuckDB and build 205 features (122 raw, 83 engineered) in plain SQL. A calibrated LightGBM model does the scoring, and a FastAPI app gives a credit officer five things: an overview of the data, a risk score with a Low, Medium or High band, a SHAP breakdown of why a particular applicant scored the way they did, a handful of readable rules distilled from the model, and a chat box where you can ask questions of the data in plain English.

I built it in a day against a brief with those five parts, and it has 119 tests.

The hard parts

I didn’t use 0.5 as the cutoff for approving a loan. Missing a default costs a lender far more than a false alarm, so I priced a miss at ten times a false alarm and picked the threshold that minimizes the expected cost.

Only about 8% of applicants default, so I tested the usual fixes for imbalanced data (SMOTE, undersampling and class weights) on the same five folds. None of them beat leaving the data alone (PR-AUC 0.279), so the final model uses none.

The chat box needed the most care, because it lets an LLM write SQL against a real database. Gemini’s query gets parsed into a syntax tree with sqlglot, checked against the actual tables and columns, capped with a row limit, and run on a read-only connection with external access turned off. There are adversarial tests for the validator.

What I had to work with

One day, and an ordinary machine. The full dataset makes a 2.37 GB database, so there’s a “lite” mode that loads a stratified sample instead. It keeps the 8.1% default rate and the links between tables, so the app still works on a smaller laptop.

How it turned out

On a holdout of 61,503 applicants, the model scores a ROC-AUC of 0.79 and a KS of 0.44. If a lender reviewed only the riskiest 30% of applicants, they’d catch 70% of everyone who defaults. Every number in the README and model card is generated from the saved results, not typed in by hand.