Sub topic 8
Reading learning curves
This chapter assumes you know train and validation splits, k-fold cross-validation, and the difference between underfitting and overfitting. It also assumes you can use scikit-learn pipelines.
What a learning curve shows
A learning curve plots model error against the number of training examples. You draw two lines. One is the error on the training data, and the other is the error on validation data. Each point comes from retraining the model on a larger slice of the data.
A single train/validation pair gives you two numbers. A learning curve shows how those numbers move as data grows. That movement tells you whether more data or a different model will help.
Computing a curve
learning_curve in scikit-learn trains the model on increasing subsets. It cross-validates each one, so the scores are averaged over folds.
import numpy as np
from sklearn.model_selection import learning_curve
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import LinearRegression
rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, size=(200, 1))
y = np.sin(X).ravel() + rng.normal(0, 0.2, size=200)
for degree in (1, 12):
model = make_pipeline(PolynomialFeatures(degree), StandardScaler(), LinearRegression())
sizes, train_scores, val_scores = learning_curve(
model, X, y,
train_sizes=[0.2, 0.4, 0.6, 0.8, 1.0],
cv=5,
scoring="neg_mean_squared_error",
)
print("degree", degree)
for n, tr, va in zip(sizes, -train_scores.mean(axis=1), -val_scores.mean(axis=1)):
print(n, round(tr, 3), round(va, 3))The scorer returns negative MSE, because scikit-learn treats higher scores as better. The code negates the values to get ordinary error.
Two shapes to recognise
High bias (underfitting). This is the degree-1 line fitted to a sine wave. Training error is high from the start. Validation error falls and then levels off close to the training error. The two lines converge at a poor value. The model cannot represent the pattern, so more data will not help. Increase capacity, add features, or reduce regularisation.
High variance (overfitting). This is the degree-12 model on a small training set. Training error is very low, and validation error is much higher. There is a gap between the lines. As the training set grows, the gap narrows, and the validation line is still falling at the right edge. More data, stronger regularisation, or a simpler model should help.
Common errors
- Judging "high" without a reference. Compare training error to a baseline, such as the noise level or human performance. In the example, the noise standard deviation is 0.2, so a floor near 0.04 MSE is the best possible result.
- Ignoring the slope. A gap that is still shrinking at the right edge means more data will help. A gap that has flattened means it will not.
- Reading noise as signal. With few folds or small data, the curves are jagged. Look at the spread across folds before drawing conclusions.
- Mixing up the axes. A learning curve varies data size. A validation curve varies a hyperparameter such as degree or
alpha. Both are useful, and they answer different questions.
Exercise
A model's training error is 0.02 and its validation error is 0.35 at full training size. The validation curve is still falling. Which problem does this show, and what are two sensible remedies?
Show answer
The model has high variance (overfitting), shown by the large gap between a low training error and a high validation error. Sensible remedies are collecting more training data, which should help because the validation curve is still falling, and increasing regularisation or reducing model capacity.