Sub topic 4
Cross-validation as the Score Estimator Behind the Curves
This chapter assumes you know how to call fit and score on a scikit-learn estimator and what a train/test split is. It explains where the numbers on a learning curve or validation curve come from. Those numbers are cross-validation scores, so the way you set up cross-validation affects every curve you draw.
Why one split is not enough
A single train/validation split gives one score. That score depends on which samples landed in the validation set, so a different random split can move it noticeably. A curve built from single splits would show noise as if it were a trend.
Cross-validation reduces this noise. It divides the data into k folds. It then trains k times, each time holding out a different fold for scoring. You get k scores per point on the curve instead of one.
What the curve functions do
learning_curve and validation_curve both run cross-validation internally. For each point on the x-axis, they do the following for every fold:
- Fit a fresh clone of the estimator on the training part of the fold.
- Score it on that same training part.
- Score it on the held-out part.
For a learning curve, the x-axis point is a training set size. The function takes only the first n samples from the fold's training part. The held-out part stays the same at every size, so the validation scores are comparable across sizes. For a validation curve, the x-axis point is a hyperparameter value, and the whole training part is used.
Setting the splitter explicitly
The cv argument accepts an integer or a splitter object. An integer uses StratifiedKFold for classifiers and KFold for everything else. Neither shuffles by default. If your rows are sorted, for example by class or by date, unshuffled folds can be very different from each other. Passing a splitter lets you control this.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, learning_curve
X, y = load_digits(return_X_y=True)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
sizes, train_scores, val_scores = learning_curve(
LogisticRegression(max_iter=2000),
X,
y,
train_sizes=np.linspace(0.1, 1.0, 5),
cv=cv,
scoring="accuracy",
)
print(train_scores.shape) # (5, 5): n_sizes by n_folds
print(val_scores.mean(axis=1)) # one validation score per size
print(val_scores.std(axis=1)) # spread across foldsEach row of a score array is one x-axis point, and each column is one fold. Averaging over axis=1 gives the curve. The standard deviation over the same axis gives the spread you will use for error bands later.
Choosing the number of folds
| Setting | Effect |
|---|---|
| Fewer folds (for example 3) | Cheaper, but each model trains on less data, so scores are pessimistic |
| More folds (for example 10) | Training parts are closer to the full data, but cost grows and each held-out part is small |
| 5 folds | A common default that balances the two |
The total number of model fits is the number of x-axis points times the number of folds. Here that is 5 × 5 = 25 fits. Costly models make this the main limit on how fine a curve you can afford.
Exercise
You call learning_curve with 6 training sizes and cv=10, then call validation_curve with 8 hyperparameter values and cv=5. How many model fits does each call perform, and what is the shape of the validation score array from validation_curve?
Show answer
learning_curve performs 6 × 10 = 60 fits. validation_curve performs 8 × 5 = 40 fits. Its validation score array has shape (8, 5): one row per hyperparameter value and one column per fold.