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:
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
MediumA telescope survey sorts sky objects into stars and galaxies. The starter splits them and sets up the chart.
- For each depth in
depths, train a tree (random_state=0) and keep both accuracies. - Print the depth with the best test accuracy as below.
- Plot both lines, labelled
trainandtest, mark the best depth with a dashedax.axvline, add a legend, show.
For a mango sorter:
Best depth: 5, test accuracy 0.867Then Coddy describes your chart. Theirs looks like this:
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
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