On this page
A model's score on the data it learned from says little about how it will do on data it has never seen. K-fold cross-validation estimates that without collecting new data. The scikit-learn user guide on cross-validation states the core warning plainly: "Learning the parameters of a prediction function and testing it on the same data is a methodological mistake."
What is k-fold cross-validation?
Split the data into k folds of roughly equal size. For each fold in turn, train on the other k − 1 folds and measure the error on the fold held out. Then average the k errors: the scikit-learn guide defines the reported score as "the average of the values computed in the loop."
Every point is tested exactly once, never by a model that saw it in training. If the data are sorted in some way, shuffle before splitting.
A worked 5-fold split of 20 points
Take 20 mock-test scores, already in random order, split into five folds of 4:
62, 71, 55, 80 | 68, 49, 74, 66 | 58, 85, 70, 61 | 77, 52, 69, 64 | 73, 57, 81, 48
The model predicts every score as the mean of its training scores. Error is mean squared error (MSE), the average of (actual − predicted)².
Round 1: train on the 16 scores outside fold 1 (mean 65.75); errors on 62, 71, 55, 80 are −3.75, 5.25, −10.75, 14.25; fold MSE 90.06.
Round 2: training mean 66.44; fold MSE 90.97.
Round 3: training mean 65.38; fold MSE 120.02.
Round 4: training mean 66.13; fold MSE 82.64.
Round 5: training mean 66.31; fold MSE 170.63.
The 5-fold estimate is the average of the five: 110.86, a typical miss of about 10.5 marks. Now train on all 20 (mean 66) and score on the same 20: the MSE is 109.5.
Why is training error optimistic?
Scoring on the training data lets the model see the answers first, which is the methodological mistake the scikit-learn guide warns about. Even this one-number model does: 109.5, against 121.33 when each score is predicted from the mean of the other 19. For an average the gap is exact: leaving a point out makes its error 20/19 times bigger, so leave-one-out MSE is always (20/19)², about 1.108, times the training MSE.
A flexible model widens the gap. One that memorises every point has zero training error and tells you nothing; the scikit-learn guide calls this overfitting.
Is leave-one-out just k = n?
Yes. With one point per fold, k-fold "is equivalent to the Leave One Out strategy" and needs n model fits instead of k, says the scikit-learn guide. It warns that "LOO often results in high variance as an estimator for the test error" and that "most authors and empirical evidence suggest that 5 or 10-fold cross validation should be preferred to LOO."
How accurate is the cross-validation estimate itself?
The estimate has its own bias and variance.
Bias: each 5-fold model trains on 16 points, not 20, so it is a little worse than your final model. The scikit-learn guide notes that when the learning curve is steep, 5 or 10-fold "can overestimate the generalization error."
Variance: the five fold scores above run from 82.64 to 170.63. Deal the students into folds differently and 110.86 moves too.
A theorem by Yoshua Bengio and Yves Grandvalet in the Journal of Machine Learning Research (2004) shows that no universal unbiased estimator of the variance of k-fold cross-validation exists. The training sets overlap, so fold errors are correlated, and naive estimates that ignore this can badly understate the variance; the arithmetic is the variance of a sum with covariance terms. So report the fold scores, not just the mean.
What is the data-leakage mistake?
Data leakage "occurs when information that would not be available at prediction time is used when building the model" (scikit-learn, common pitfalls). The classic slip is to select features or scale using all the data, then cross-validate: the test fold has already shaped the model. In the guide's example, selecting features before splitting scored 0.76 on purely random labels, against the honest 0.5.
It has happened in print. Christophe Ambroise and Geoffrey McLachlan showed in PNAS (2002) that published gene-expression rules for cancer diagnosis, reporting negligible error with only a few genes, had chosen those genes outside the cross-validation loop; once the selection bias was corrected, the cross-validated error was no longer zero. They recommended 10-fold over leave-one-out cross-validation, or the .632+ bootstrap error estimate.
Before you trust the number:
Shuffle, then split into k = 5 or 10 folds.
Fit every preprocessing step inside each fold.
Report the mean and the spread of fold scores.
Keep a final test set untouched; the scikit-learn guide says one "should still be held out for final evaluation."
Then try it on a real classifier such as logistic regression. If MSE, bias or variance feel shaky, the Fermi Concept Library gives every concept in mathematics and computing its own lesson. To check your fold arithmetic, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



