Sub topic 10
Fold-to-fold variability and error bands
This chapter assumes you can run learning_curve and validation_curve and know what a cross-validation fold is. A curve point is an average over folds, so it hides how much the folds disagreed. Here you will learn to measure that disagreement and to decide when a gap between two curves is real.
Why a single mean is not enough
Both functions return score arrays with one row per setting (training size or hyperparameter value) and one column per fold. Averaging over axis=1 gives one number per row. Each fold uses a different split, so each column is a slightly different estimate of the same quantity.
Small datasets, rare classes, and noisy labels all make folds disagree more. If the spread is large, two curves that look separated by their means may overlap once you account for it.
Computing the spread
The standard deviation across folds measures this spread. The code below computes it and plots a band of one standard deviation around each mean.
import numpy as np
import matplotlib.pyplot as plt
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",
)
train_mean = train_scores.mean(axis=1)
train_std = train_scores.std(axis=1)
val_mean = val_scores.mean(axis=1)
val_std = val_scores.std(axis=1)
fig, ax = plt.subplots()
ax.plot(train_sizes, train_mean, marker="o", label="Training score")
ax.fill_between(train_sizes, train_mean - train_std, train_mean + train_std, alpha=0.2)
ax.plot(train_sizes, val_mean, marker="o", label="Cross-validation score")
ax.fill_between(train_sizes, val_mean - val_std, val_mean + val_std, alpha=0.2)
ax.set_xlabel("Number of training samples")
ax.set_ylabel("Accuracy")
ax.legend()
plt.show()The shaded region shows where a typical fold lands. A wide validation band means the estimate is unstable. A narrow training band next to a wide validation band is common and is itself a sign of variance.
Standard deviation versus standard error
The standard deviation describes the spread of individual folds. The standard error of the mean is std / sqrt(n_folds), and it describes how uncertain the mean is. Plotting one standard deviation is the usual convention because it is easy to read. If you want to claim that the mean itself differs between two settings, the standard error is closer to the right measure.
Neither is exact. Training sets in different folds overlap heavily, so the fold scores are not independent. The standard error therefore understates the true uncertainty, and you should treat the bands as a rough guide.
Reading the bands
- If the bands of two settings overlap heavily, do not treat the difference between their means as meaningful.
- A band that narrows as training size grows suggests the estimate is stabilizing.
- A band that stays wide at the largest size suggests more data or repeated cross-validation may help.
Reducing the noise
You can make the estimate steadier in a few ways:
- Use
RepeatedStratifiedKFoldfor classification so each point averages over more splits. - Increase the number of folds, which also raises the cost.
- Fix
random_stateso that comparisons between runs are reproducible.
Common mistakes
- Reporting only the mean and drawing conclusions from a small gap.
- Reading the band as a confidence interval with a stated probability. It is a plain spread, not a calibrated interval.
- Comparing bands from different
cvsettings as if they were equivalent.
Exercise
A validation curve shows a mean score of 0.91 at one hyperparameter value and 0.92 at the next. The standard deviation across folds is about 0.03 at both. Which value should you pick on the basis of the scores alone, and why?
Show answer
Neither can be preferred on the scores alone. The 0.01 difference is much smaller than the 0.03 spread, so the bands overlap almost completely. Prefer the simpler or cheaper setting, or repeat the cross-validation with more splits to see whether the gap holds.
Exercise
Why does the standard error std / sqrt(n_folds) tend to understate the uncertainty of a cross-validation mean?
Show answer
The training sets of different folds overlap heavily, so the fold scores are correlated rather than independent. The formula assumes independence, so it gives a value that is too small.