Sub topic 7
Reading a Learning Curve
This chapter assumes you can already compute a learning curve with learning_curve and that you know the difference between a training score and a cross-validated score. Here the focus is on interpretation. You will learn what the level, the gap, and the slope of each curve tell you, and what they can't tell you.
What a learning curve measures
A learning curve has two lines. The training score is measured on the samples the model was fitted on. The validation score is measured on held-out folds. Both are plotted against the number of training samples.
Three features carry nearly all the information:
- Level: how high the validation curve sits compared with the score you need.
- Gap: the distance between the training and validation curves at the largest training size.
- Slope: whether the validation curve is still rising at the right edge, or has flattened.
Read them in that order. The level tells you whether there is a problem. The gap tells you what kind of problem it is. The slope tells you whether collecting more data would help.
Why the curves move the way they do
With very few training samples, a flexible model can memorize them, so the training score starts high. A model that has memorized a handful of points generalizes poorly, so the validation score starts low.
As the training set grows, memorizing becomes harder and the training score usually falls. The validation score usually rises because the model sees more of the underlying pattern. The curves move toward each other.
Where they end up depends on the model. They settle near a ceiling set by the model's capacity and by label noise in the data.
The three standard shapes
High bias (underfitting)
Both curves are low and they converge. The gap is small, often only a few points. The validation curve flattens early.
The model is too simple to capture the pattern, even on the data it was fitted on. The training score is poor because the model cannot fit the data. Adding samples doesn't change that.
High variance (overfitting)
The training curve stays high, near its maximum. The validation curve is much lower, and a wide gap separates them. The validation curve is usually still rising at the right edge.
The model fits the training samples, including their noise, but does not carry over to new data. More data narrows the gap, slowly.
A good fit
Both curves are high and close together, and the validation curve has flattened. A small remaining gap is normal. The training score is nearly always a little higher than the validation score.
| Pattern | Training score | Validation score | Gap | Validation slope at the right edge |
|---|---|---|---|---|
| High bias | Low | Low | Small | Flat |
| High variance | High | Much lower | Large | Often still rising |
| Good fit | High | High | Small | Flat or nearly flat |
Comparing the patterns in code
The script below reuses learning_curve on three models and prints the numbers that matter for reading: the final gap and the last-step gain in the validation score. The shallow tree should show high bias, and the RBF SVC with a large gamma should show high variance. The SVC with a small gamma should be a reasonable fit.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
X, y = load_digits(return_X_y=True)
models = {
"shallow tree (expect high bias)": DecisionTreeClassifier(max_depth=2, random_state=0),
"SVC gamma=0.01 (expect high variance)": SVC(gamma=0.01),
"SVC gamma=0.001 (expect good fit)": SVC(gamma=0.001),
}
for name, model in models.items():
sizes, train_scores, val_scores = learning_curve(
model,
X,
y,
train_sizes=np.linspace(0.2, 1.0, 5),
cv=5,
scoring="accuracy",
shuffle=True,
random_state=0,
)
train_mean = train_scores.mean(axis=1)
val_mean = val_scores.mean(axis=1)
print(name)
print(f" train={train_mean[-1]:.3f} validation={val_mean[-1]:.3f}")
print(f" gap={train_mean[-1] - val_mean[-1]:.3f}")
print(f" validation gain over last step={val_mean[-1] - val_mean[-2]:+.3f}")Read the output with the three features in mind. For the tree, both scores are low and the gap is small. For the large-gamma SVC, the training score is near 1.0 while the validation score is far lower. For the small-gamma SVC, both are high and the gain over the last step is close to zero.
Does more data help?
This is the question learning curves answer best. Look at the validation curve over the last two or three training sizes.
- If it is still climbing, the model has not yet extracted all it can from the data. More samples are likely to raise the validation score, and the gap should shrink.
- If it is flat and the training curve has also flattened, more data of the same kind will not help. Change the model, the features, or the regularization instead.
The slope is a hint, not a forecast. The curve covers sizes up to the size of your current training split, so extrapolating much beyond that is guesswork. If the curve is rising, a doubling of the data might help noticeably, but the curve cannot say by how much.
The level matters as much as the shape
A small gap does not mean the model is good. It means the model is consistent between training and validation data. Two curves that converge at 60% accuracy describe a consistent but weak model.
Always compare the validation level with something external:
- the score a trivial baseline achieves, such as
DummyClassifier - a business or product requirement
- an estimate of human performance or the label noise ceiling
If the training score is close to a known ceiling, such as the noise level in the labels, a gap may be unavoidable and not worth chasing.
Reading the spread
The shaded bands show how much scores vary between folds. Interpret them with care:
- Wide bands at small training sizes are normal. Few samples give noisy fits.
- Bands that stay wide on the validation curve suggest the folds differ in difficulty. Class imbalance, small data, or dependent samples are common causes.
- Overlapping bands between two runs mean you cannot tell their means apart. The bands are not a formal confidence interval, because the folds share training data and are not independent.
A difference between two models that is smaller than the fold-to-fold spread is not strong evidence that one model is better.
Common misreadings
- Treating a high training score as success. The training score only shows that the model can fit the data it saw. It says nothing about new data.
- Calling a noisy curve a trend. If the curve zigzags by about the size of the band, the zigzag is noise. Repeat with a different
random_stateor more folds before drawing a conclusion. - Reading a small gap as proof that more data is useless. A small gap with a low level shows high bias. Another sample of the same kind will not help there, but better features or a richer model might.
- Ignoring the metric. Accuracy can look healthy on imbalanced data while the model fails on the rare class. Check that the
scoringargument matches the goal. - Reading a validation curve that is above the training curve. A validation score that beats the training score usually points to a problem, such as an easy validation split, leakage in the way the scores were built, or regularization that acts differently at training time. Investigate before trusting the plot.
Summary of what to do next
After reading the curve, you have a diagnosis. High bias calls for a more flexible model or better features. High variance with a rising validation curve calls for more data. High variance with a flat validation curve calls for stronger regularization or a simpler model. The "From diagnosis to action" chapter works through these remedies in detail.
Exercise
A learning curve for a classifier shows a training accuracy of 0.99 and a validation accuracy of 0.72 at the largest training size. The validation curve has risen by about 0.03 over the last two training sizes. What is the diagnosis, and what are two reasonable next steps?
Show answer
This is high variance (overfitting): the gap is 0.27 and the training score is near its maximum. Because the validation curve is still rising, collecting more training data is a reasonable first step. A second step is to reduce model flexibility, for example by increasing regularization or limiting model complexity, then recompute the curve to check that the gap narrows.
Exercise
Both curves of a model converge at about 0.65 accuracy, the gap is under 0.02, and both curves are flat over the last three training sizes. A colleague proposes labeling twice as much data. Is that likely to help, and what would you try instead?
Show answer
It is unlikely to help. This is a high-bias pattern: the model is too limited to fit even the training data, and the flat curves show that more samples of the same kind do not change that. Try a more flexible model, add or engineer features, or reduce regularization, then compute the learning curve again.