Recap: Tuning a Tree
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 36 of 58.
Challenge
MediumA roaster's sensors read 6 values per coffee batch; 1 means ready to bag. Choose a tree depth honestly.
- Split off 20% for testing, then 25% of the rest for validation (
random_state=0). - For depths 1 to 10, fit a tree (
random_state=0) on training rows; plot its training and validation accuracy,marker="o", labelledtrainingandvalidation. - Print the best depth, then its test accuracy.
- 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.750and draws this. Coddy then describes your chart.
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
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