Sub topic 14
How noisy is the estimate? Repeating nested cross-validation
This chapter assumes you can run the nested cross-validation from the previous chapters: GridSearchCV inside cross_val_score, with preprocessing in a Pipeline. It adds one question. If you ran the whole procedure again with different random splits, how much would the score change?
Two sources of noise
A nested score depends on the random fold assignment. With a small dataset, one shuffle can give 0.972 and another 0.965. That is split noise. You can reduce it by averaging over several shuffles.
The score also depends on which samples you happened to collect. That is sampling noise. Repeating the splits does not reduce it, because every repeat uses the same rows.
| Source | Cause | Reduced by repeating? |
|---|---|---|
| Split noise | Random assignment of rows to folds | Yes |
| Sampling noise | The finite dataset itself | No |
Repeating the whole procedure
Change the random state of both the inner and outer splitters on each repeat. Then record one nested score per repeat.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV, StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_breast_cancer(return_X_y=True)
pipe = make_pipeline(StandardScaler(), SVC())
param_grid = {"svc__C": [0.1, 1, 10], "svc__gamma": ["scale", 0.01, 0.001]}
repeat_means = []
for seed in range(5):
inner_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
outer_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=100 + seed)
search = GridSearchCV(pipe, param_grid, cv=inner_cv)
scores = cross_val_score(search, X, y, cv=outer_cv)
repeat_means.append(scores.mean())
print(seed, round(scores.mean(), 4))
repeat_means = np.array(repeat_means)
print(f"mean over repeats: {repeat_means.mean():.4f}")
print(f"spread between repeats (std): {repeat_means.std(ddof=1):.4f}")The mean over repeats is your reported estimate. The standard deviation between repeats shows how much a single nested run could move. If it is much smaller than the differences between models you are comparing, one run was probably enough.
Reading the spread honestly
- The spread between repeats measures split noise only. It is not a confidence interval for performance on new data.
- Do not treat the outer-fold scores as independent. The training sets overlap heavily, so the usual
std / sqrt(n)standard error is too small. - The standard deviation of outer-fold scores inside one run mostly reflects differences in difficulty between test folds. Report it, but do not confuse it with the repeat spread.
What it costs
Cost grows linearly with repeats. With 9 candidates, 5 inner folds, and 5 outer folds, one nested run needs 5 × (9 × 5 + 1) = 230 fits. Five repeats need 1,150. Spend repeats where they matter: small datasets, close model comparisons, or a final reported number. Use n_jobs on cross_val_score or GridSearchCV to parallelize, and keep the grid small while exploring.
Exercise
Your single nested run gives 0.95. Five repeats with reshuffled splits give a mean of 0.95 and a repeat standard deviation of 0.004. A colleague says this proves the model will score within 0.95 ± 0.008 on new data. What is wrong with that claim?
Show answer
The repeat spread measures only split noise, meaning how much the score moves when the same data is divided differently. It says nothing about sampling noise, because every repeat reuses the same rows. The real uncertainty on new data is larger and cannot be read from this number.