Sub topic 7
Reading a learning curve
This chapter assumes you know how to compute a learning curve with learning_curve, and that you understand training versus validation scores, overfitting, and cross-validation. Here you learn to read the shape of the curves and decide what they tell you.
What the two curves show
A learning curve plots two lines against the number of training samples. The training score is measured on the data the model was fit on. The validation score is the cross-validated score on held-out data. Read three things: where each line ends, how far apart they are, and whether they are still moving.
The code below computes the curves and prints the means, as in the previous chapter.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import learning_curve
X, y = load_digits(return_X_y=True)
train_sizes, train_scores, val_scores = learning_curve(
LogisticRegression(max_iter=2000),
X,
y,
train_sizes=np.linspace(0.1, 1.0, 5),
cv=5,
scoring="accuracy",
)
for n, tr, va in zip(train_sizes, train_scores.mean(axis=1), val_scores.mean(axis=1)):
print(f"n={n:4d} train={tr:.3f} validation={va:.3f} gap={tr - va:.3f}")The two basic patterns
Most curves fall into one of two shapes.
High bias (underfitting)
Both curves flatten out close together at a score you consider too low. The model is too simple to capture the pattern, so extra data does not help. The training score starts high with very few samples and falls as the model can no longer memorize them. The validation score rises and meets it.
High variance (overfitting)
The training score stays high, often near perfect. The validation score is clearly lower, leaving a wide gap. The model fits noise in the training set that does not generalize.
The deciding question is whether the validation curve is still rising at the right edge. If it is, more data is likely to shrink the gap. If it has flattened, more data of the same kind will add little.
A quick reference
| What you see | Likely cause | First thing to try |
|---|---|---|
| Both curves low and close together | High bias | A more flexible model, better features, less regularization |
| Large gap, validation still rising | High variance, data-limited | Collect more data |
| Large gap, validation flat | High variance, not data-limited | Simpler model, stronger regularization, fewer features |
| Small gap, high scores | Good fit | Stop tuning, or improve the metric you care about |
Common reading errors
Several mistakes recur when people read these plots.
- Ignoring the scale. A gap of 0.01 on an accuracy axis that runs from 0.95 to 1.0 looks dramatic but is small. Check the axis limits before drawing conclusions.
- Judging from one point. A single training size is noisy. Look at the trend across all sizes.
- Ignoring fold spread. If the bands from the standard deviation overlap, a small difference between curves may be noise.
- Extrapolating too far. Curves tell you what happened up to your largest training size. A rising curve does not promise a given gain from doubling the data.
- Comparing a perfect training score to a good validation score and calling it a bug. Flexible models such as deep trees reach 1.0 training accuracy routinely. What matters is the gap.
Putting it together
- Check the final validation score. Is it acceptable for your task?
- Check the gap between the curves at the largest size.
- Check whether the validation curve is still rising.
- Match the result to a row in the table above.
The next chapter turns these diagnoses into concrete actions.
Exercise
A learning curve shows a training accuracy of 0.99 and a validation accuracy of 0.80 at the largest training size. The validation curve has been flat for the last three training sizes. Which problem does this indicate, and is collecting more data the best first step?
Show answer
This is high variance (overfitting), because the gap is large and training is near perfect. Because the validation curve is flat, more data of the same kind is unlikely to help much. A better first step is to reduce model complexity, increase regularization, or remove features.
Exercise
Both the training and validation curves converge at an accuracy of 0.70, and your target is 0.90. Why will doubling the dataset probably not reach the target?
Show answer
The curves have converged with a small gap, which indicates high bias. The model is too simple for the pattern, so more samples will not raise either score much. You need a more expressive model, better features, or weaker regularization.