Recap: Loan Decisions
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 31 of 58.
Challenge
MediumRemember the farm loan this chapter promised? A microloan fund wants a tree it can explain to farmers. The starter splits its past loans.
- Grow trees from depth 1 up (
random_state=0), printing each test accuracy. Stop at the first depth that does no better than the one before. - Keep the last depth that improved: print it and that tree's questions.
- Print its two most important features, as below.
For another fund:
Depth 1: test accuracy 0.650
Depth 2: test accuracy 0.850
Depth 3: test accuracy 0.833
Keep depth 2
|--- savings_usd <= 154.50
| |--- earlier_loans <= 0.50
| | |--- class: 0
| |--- earlier_loans > 0.50
| | |--- class: 1
|--- savings_usd > 154.50
| |--- earlier_loans <= 0.50
| | |--- class: 1
| |--- earlier_loans > 0.50
| | |--- class: 1
Most important: savings_usd (0.563)
Next: earlier_loans (0.437)Try it yourself
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text
# Past microloans to small farms: 1 in repaid means the loan was paid back in full
loans = pd.read_csv("loans.csv")
features = ["amount_usd", "income_usd", "years_farming", "earlier_loans", "savings_usd", "group_size"]
X_train, X_test, y_train, y_test = train_test_split(loans[features], loans["repaid"], test_size=0.25, random_state=0)
# Write your code below
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