Menu
CoddyTech

Credit Approval

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 56 of 58.

A cooperative bank runs a store card. In cards.csv, 79 of its 320 holders fell behind on payments in their first year:

Density histograms of debt ratio for 241 holders who kept up, mostly between 0.1 and 0.35, and 79 who fell behind, shifted right toward 0.3 to 0.8, with a wide overlap

Debt helps, but the groups overlap. The bank will score each applicant's risk and send anyone above a threshold to a person for review.

Its rule: catch a set share of future late payers. Pick the threshold on validation rows, then judge it once on the test rows.

challenge icon

Challenge

Hard

The program reads the recall the bank wants, target. The starter lists the columns and thresholds, highest first.

  1. Split off a stratified test quarter, then a stratified validation quarter of what remains, both random_state=0.
  2. Fit a pipeline on the training rows left: median fill and scale the numbers, one-hot home, logistic regression.
  3. On validation, take the first threshold whose recall reaches target.
  4. Print it, then how the test rows fare at it, as below (3 decimals).

For another bank and 0.7:

Threshold: 0.3 (validation recall 0.733)
Test: caught 15 of 21 late payers, 12 false alarms
Precision: 0.556

Try it yourself

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import recall_score, precision_score, confusion_matrix

cards = pd.read_csv("cards.csv")
numbers = ["age", "income_k", "debt_ratio", "years_at_job", "late_payments", "card_limit_k"]
text = ["home"]
X = cards[numbers + text]
y = cards["fell_behind"]

target = float(input())
thresholds = [0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1]

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler