Bootstrap sampling and statistics in machine learning both mention cross-validation as a way to test how well a model generalizes to new data, without walking through how it's actually done. K-fold cross-validation repeats the basic train/test idea multiple times over different slices of the same dataset, averaging the results into a single, more reliable performance estimate than any one train/test split alone could provide.
- How k-fold cross-validation splits and rotates through the data
- A full worked example computing a 5-fold cross-validated error estimate by hand
- Why averaging across folds gives a more honest estimate than a single train/test split
- How to choose k, and the tradeoff involved
How K-Fold Cross-Validation Works
The full dataset is randomly split into k roughly equal-sized groups, called folds. The model is then trained k separate times: each time, one fold is held out as the test set, and the model is trained on the remaining kβ1 folds combined. Every fold gets used as the test set exactly once, and the resulting k performance scores are averaged into a single overall estimate.
Worked Example
Problem: A dataset of 10 values: 10, 12, 11, 15, 9, 14, 13, 8, 16, 10. Using a simple model that predicts the training set's own mean for every test point (a deliberately simple baseline, chosen so the whole calculation can be done by hand), estimate the model's mean squared error (MSE) using 5-fold cross-validation (2 points per fold).
| Fold | Test values | Training mean | Fold MSE |
|---|---|---|---|
| 1 | 10, 12 | 12.000 | 2.000 |
| 2 | 11, 15 | 11.500 | 6.250 |
| 3 | 9, 14 | 11.875 | 6.391 |
| 4 | 13, 8 | 12.125 | 8.891 |
| 5 | 16, 10 | 11.500 | 11.250 |
For Fold 1, for example: the training mean is computed from the other 8 points (everything except 10 and 12), giving 12.000; the fold's MSE is the average of (10β12.000)Β² and (12β12.000)Β², which is (4+0)/2 = 2.000. Each fold repeats this process with a different pair held out.
Averaging all 5 fold MSE values: (2.000+6.250+6.391+8.891+11.250)/5 = 6.956. This single number β the 5-fold cross-validated MSE β is the model's overall performance estimate, built from testing on every single data point exactly once, while always training on data the model hadn't seen for that particular prediction.
Why Averaging Across Folds Matters
A single train/test split's performance estimate depends heavily on which particular points happened to land in the test set β an unusually easy or unusually hard test set, purely by chance, can make a model look better or worse than it really is. Notice in the worked example how much the fold MSEs vary (2.000 to 11.250) even though the same simple model and the same underlying dataset are used throughout β this variation is exactly why relying on just one such split would be unreliable. Averaging across k different splits smooths out this luck-of-the-draw variability, giving a more stable, trustworthy estimate of how the model is likely to perform on genuinely new data. The individual fold results are also informative in their own right: a small standard deviation across the 5 fold MSEs would suggest a stable, consistent model, while a large spread (as seen here, with MSEs ranging from 2.000 to over 11) suggests the model's performance depends meaningfully on which specific data happens to be available for training.
Choosing K: A Real Tradeoff
A larger k (more folds, each smaller) means each individual training set is closer in size to the full dataset, generally giving a less biased performance estimate β but it also means more separate model fits are needed, and each test fold is smaller, making individual fold results noisier. A smaller k is computationally cheaper but leaves more data out of each training run, potentially giving a pessimistic performance estimate relative to what the model would achieve when finally trained on the full dataset. k=5 and k=10 are the most common choices in practice, both reasonable compromises between these two costs; the extreme case of k=n (one fold per single data point) is called leave-one-out cross-validation, using the maximum possible training data per fold at the cost of the most computation.
Real-World Uses
Comparing candidate models before deployment: when choosing between several possible models or sets of features, cross-validation gives each candidate a fair, consistent performance estimate on the same data, making it the standard way to decide which option is actually likely to perform best on new, unseen data.
Tuning model settings: many machine learning models have adjustable settings (like the Ξ» regularization strength in ridge regression) that need to be chosen using the data itself β cross-validation is the standard way to test several candidate settings and pick the one that generalizes best, rather than simply the one that fits the training data most closely.
Reporting honest performance in research and reports: whenever a model's accuracy, error rate, or other performance figure is reported, a cross-validated estimate is considerably more trustworthy than a number computed only on the same data the model was trained on, which tends to look artificially strong.
Common Mistakes
Letting information leak between folds. Any data preparation step that uses information from the full dataset (like calculating a scaling factor from all the data before splitting into folds) can let information from the test fold subtly influence the training process, producing an overly optimistic performance estimate β such preparation steps should be redone separately within each fold's training data only.
Treating cross-validation as a way to improve a model. Cross-validation estimates how well a model is likely to generalize β it doesn't change the model itself; genuinely improving performance requires changing the model, its features, or its training approach, with cross-validation used afterward to honestly evaluate whether that change actually helped.
Is cross-validation the same as the train/test split covered in statistics in machine learning?
Is cross-validation the same as bootstrapping?
Does cross-validation work for classification models too, not just numeric predictions?
What is stratified k-fold cross-validation?
Share this article
Written by
Statistics Fundamentals Editorial Team
Written and reviewed by our team of statistics educators. Content is aligned with GCSE, A-Level, AP Statistics, and undergraduate curricula.
About Statistics Fundamentals β