Menu
CoddyTech

Tree Depth

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

A tree can keep asking until every training row has its own answer. Then it has memorized, not learned:

Train and test accuracy of trees from depth 1 to 12 on noisy half-moon data. Train climbs to 1.0 by depth 8, labelled memorizing. Test peaks at 0.907 at depth 6, marked by a dashed line, then falls to about 0.87

Deep trees learn the noise of their own training rows, so test accuracy peaks, then falls. Try several depths:

train = []
test = []
for d in range(1, 13):
    tree = DecisionTreeClassifier(max_depth=d, random_state=0)
    tree.fit(X_train, y_train)
    train.append(tree.score(X_train, y_train))
    test.append(tree.score(X_test, y_test))

On these moons, depth 6 does best on new rows: keep it, for now. From depth 8, the tree is perfect on its memorized rows, worse on the rest.

challenge icon

Challenge

Medium

A telescope survey sorts sky objects into stars and galaxies. The starter splits them and sets up the chart.

  1. For each depth in depths, train a tree (random_state=0) and keep both accuracies.
  2. Print the depth with the best test accuracy as below.
  3. Plot both lines, labelled train and test, mark the best depth with a dashed ax.axvline, add a legend, show.

For a mango sorter:

Best depth: 5, test accuracy 0.867

Then Coddy describes your chart. Theirs looks like this:

A chart titled Ripe or Not? with tree depth 1 to 10 across: a blue train line climbing to about 0.98, an orange test line peaking near 0.87 at depth 5 and a dashed vertical line there, with a legend

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# One row per sky object in a telescope survey: 1 in galaxy means a galaxy, 0 a star
sky = pd.read_csv("sky.csv")
features = ["brightness_mag", "size_arcsec", "roundness", "color_index"]
X_train, X_test, y_train, y_test = train_test_split(sky[features], sky["galaxy"], test_size=0.25, random_state=0)
depths = range(1, 11)

fig, ax = plt.subplots()
ax.set_title("Star or Galaxy?")
ax.set_xlabel("Tree depth")
ax.set_ylabel("Accuracy")

# 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