Sub topic 12
Repeated Cross-Validation
This chapter assumes you know how k-fold and stratified k-fold work, and that you can run cross_val_score on a scikit-learn pipeline.
Why one k-fold run is still noisy
K-fold cross-validation shuffles the data once and cuts it into k folds. A different shuffle puts different samples together in each test fold, so the fold scores change. On a small dataset, two runs with different seeds can give means that differ by more than the gap between two models you are comparing.
Repeated cross-validation runs the whole k-fold procedure several times, each time with a fresh shuffle. You then pool all the fold scores. With 5 folds and 10 repeats you get 50 scores from 50 model fits.
Running it in scikit-learn
Scikit-learn provides RepeatedKFold and RepeatedStratifiedKFold. Use the stratified version for classification, for the same reasons as plain stratified k-fold. Pass the splitter to cross_val_score as cv.
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedStratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=0)
scores = cross_val_score(model, X, y, cv=cv, scoring="accuracy")
print(len(scores))
print(f"mean={scores.mean():.3f} std={scores.std():.3f}")The first print shows 50. The pipeline refits the scaler inside every training fold, so repeating does not introduce leakage.
What repetition fixes and what it does not
Repetition reduces the variance that comes from one arbitrary partition of the data. The mean over 50 scores depends much less on the seed than the mean over 5.
It does not give you new data. Every repeat uses the same samples, so the result is still limited by how well your dataset represents the real population. It also does not change the bias that comes from training on only (k-1)/k of the data. That is a property of k.
| Property | Single k-fold | Repeated k-fold |
|---|---|---|
| Model fits | k | k × n_repeats |
| Sensitivity to the shuffle seed | High | Lower |
| Bias from training size | Set by k | Same as single k-fold |
| Information about new data | None | None |
Reporting the spread correctly
The 50 scores are not independent. Training sets overlap heavily within a repeat, and the same samples are tested again in every repeat. Two consequences follow:
- The standard deviation across the pool describes how much scores vary between folds. It is a reasonable summary to report.
- Do not compute
std / sqrt(50)and call it a standard error. It is too small, because it treats the scores as 50 independent measurements.
Comparing two models
Create one splitter and pass the same object to both models. With an integer random_state, the splitter generates the same splits on every call. Both models are then scored on identical folds, so you can compare them fold by fold. Subtract the scores pairwise and look at the mean and spread of the differences. If you use different seeds for the two models, you add partition noise to the comparison.
When to use it
- Use it when the dataset is small or medium and a fit is cheap. Typical settings are 5×5 or 10×3.
- Skip it when a single fit takes hours. The cost grows linearly with
n_repeats, and a larger dataset already makes one run stable. - Between 3 and 10 repeats is usually enough. Beyond that, the mean barely moves.
Exercise
You run RepeatedStratifiedKFold with nsplits=5 and nrepeats=4, then compute std/sqrt(20) as the standard error of the mean accuracy. Name the number of model fits and say why this standard error is unreliable.
Show answer
The run performs 5 × 4 = 20 fits. The standard error is unreliable because the 20 scores are not independent: training sets overlap within each repeat, and the same samples are tested again in every repeat. Dividing by sqrt(20) treats them as 20 independent measurements, so the interval comes out too narrow.