Menu
CoddyTech

Recap: Loan Decisions

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

challenge icon

Challenge

Medium

Remember the farm loan this chapter promised? A microloan fund wants a tree it can explain to farmers. The starter splits its past loans.

  1. 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.
  2. Keep the last depth that improved: print it and that tree's questions.
  3. 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

Practice on your own: Online Python compiler