Learning Curves
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 33 of 58.
Would more data make your model better? A learning curve shows it: test accuracy as the training set grows. Here, a deep tree on two-moons data:
The tree memorizes every training set, yet each extra row teaches it something real. Flexible models gain most from data.
Fit on the first n training rows, then score on the same test rows each time:
sizes = [25, 50, 100, 200, 400, 800]
test_scores = []
for n in sizes:
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train[:n], y_train[:n])
test_scores.append(tree.score(X_test, y_test))
ax.plot(sizes, test_scores, marker="o")Still climbing at the right edge? More data will help.
Challenge
MediumWould more recordings help tell two owl species apart? The starter splits 600 calls and readies both models and the chart.
- For each
ninsizes, fit both models on the firstntraining rows; keep their test accuracies. - Print each model's gain, last score minus first, 3 decimals.
- Plot both against
sizes,marker="o", labelledlogisticandtree, with a legend.
Owls from another wood print:
Logistic gains 0.053 from more data
Tree gains 0.160 from more dataand draw this chart. Coddy then describes yours.
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
# Don't change the lines below: 600 owl calls, pitch (Hz) and length (s)
rng = np.random.default_rng(23)
pitch = np.round(rng.uniform(300, 900, 600))
length = np.round(rng.uniform(0.2, 2.0, 600), 2)
# Species 1, the tawny owl, calls longer for its pitch; a few field notes are wrong
longer = length > 0.5 + 6.4 * ((pitch - 600) / 600) ** 2
wrong_note = rng.random(600) < 0.05
species = np.where(longer != wrong_note, 1, 0)
X = np.column_stack([pitch, length])
X_train, X_test, y_train, y_test = train_test_split(X, species, test_size=0.25, random_state=0)
sizes = [25, 50, 100, 200, 400]
logistic = LogisticRegression(max_iter=1000)
tree = DecisionTreeClassifier(random_state=0)
fig, ax = plt.subplots()
ax.set_title("Owl Calls: Learning Curves")
ax.set_xlabel("Training rows")
ax.set_ylabel("Test 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