Menu
CoddyTech

Recap: Tuning a Tree

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

challenge icon

Challenge

Medium

A roaster's sensors read 6 values per coffee batch; 1 means ready to bag. Choose a tree depth honestly.

  1. Split off 20% for testing, then 25% of the rest for validation (random_state=0).
  2. For depths 1 to 10, fit a tree (random_state=0) on training rows; plot its training and validation accuracy, marker="o", labelled training and validation.
  3. Print the best depth, then its test accuracy.
  4. Mark that depth with a dashed line and the test score with a red star; add a legend.

Another roaster prints:

Best depth: 5 (validation accuracy 0.887)
Test accuracy: 0.750

and draws this. Coddy then describes your chart.

Coffee Batches: Choosing a Depth. The training line climbs from 0.69 to almost 1.0; the validation line peaks at 0.89 at depth 5, then falls to 0.78. A dashed line stands at depth 5 with a red star on it at 0.75, the test score

Try it yourself

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# make_classification makes up realistic rows for practice: here 400 coffee batches,
# 6 roaster sensor readings each, label 1 when the batch is ready to bag
X, y = make_classification(n_samples=400, n_features=6, n_informative=3, n_redundant=1,
                           flip_y=0.1, random_state=79)

fig, ax = plt.subplots()
ax.set_title("Coffee Batches: Choosing a Depth")
ax.set_xlabel("Tree depth")
ax.set_ylabel("Accuracy")

# Write your code below

All lessons in Supervised Learning

Practice on your own: Online Python compiler