Menu
CoddyTech

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:

A tree diagram. The top box asks: petal width 0.8 cm or less? Yes leads to setosa. No leads to a second box: petal length 4.95 cm or less? Yes leads to versicolor, no to virginica

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 icon

Challenge

Medium

A 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.

  1. Train a decision tree with at most 2 questions per path (random_state=0).
  2. Print its test accuracy (3 decimals) and its answer for the new rental: likely late for 1, likely on time for 0.

For another shop:

Test accuracy: 0.792
This rental: likely on time

Try 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
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