Trees Ask Questions
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 27 of 58.
This chapter ends with a tree that explains a farm loan. A decision tree plays twenty questions: one yes-or-no question about a feature at a time, down to an answer:
Training picks each question so its answers split the classes as cleanly as possible:
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=2, random_state=0)
tree.fit(X_train, y_train) # petal length and width, cm
print(tree.predict([[4.5, 1.4]]))This prints [1], versicolor. max_depth=2 allows two questions per path. No scaling needed: each question reads one feature.
Challenge
MediumA camera rental shop texts a reminder when gear may come back late. The starter splits its past rentals and reads a new rental into the one-row table new_rental.
- Train a decision tree with at most 2 questions per path (
random_state=0). - Print its test accuracy (3 decimals) and its answer for the new rental:
likely latefor 1,likely on timefor 0.
For another shop:
Test accuracy: 0.792
This rental: likely on timeTry it yourself
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
# Past rentals: 1 in late means the gear came back late
rentals = pd.read_csv("rentals.csv")
features = ["rental_weeks", "gear_value_usd", "customer_age", "late_before"]
X_train, X_test, y_train, y_test = train_test_split(rentals[features], rentals["late"], test_size=0.25, random_state=0)
# A new rental: weeks booked, the gear's value, the customer's age and their late returns so far
weeks = int(input())
value = int(input())
age = int(input())
late_before = int(input())
new_rental = pd.DataFrame([[weeks, value, age, late_before]], columns=features)
# 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