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:
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
HardThe program reads the recall the bank wants, target. The starter lists the columns and thresholds, highest first.
- Split off a stratified test quarter, then a stratified validation quarter of what remains, both
random_state=0. - Fit a pipeline on the training rows left: median fill and scale the numbers, one-hot
home, logistic regression. - On validation, take the first threshold whose recall reaches
target. - 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.556Try 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler