k-Fold Cross-Validation Procedure
In each iteration, one fold is set aside as the test set and the model is fitted on the remaining folds.
Try it: K-Fold Cross-Validation and Model Selection
How hold-out and k-fold cross-validation choose among candidate models: each fold takes a turn as the validation set, the fold errors are averaged, the lowest average picks the model, and only then is the untouched test set used once — while tuning on the test set would report an optimistic number.
How it works
- Set 10 test points aside; they take no part in any choice.
- Optionally shuffle the 20 development points (seeded), then cut them into k folds (hold-out: one chunk validates, the rest trains).
- For every round, train each candidate polynomial on the other folds and measure its validation MSE on the held-out fold.
- Average each candidate's fold errors (with repeated shuffling: average the p run scores; p × k fits per candidate).
- Select the candidate with the lowest average, retrain it on all development points, and evaluate it once on the test set.
- For contrast, show which candidate the test set itself would have picked — that figure is leaked and optimistic.
Default run (12 steps): 20 development points (sorted by x as they arrived) and 10 test points set aside. Candidates: polynomial degree 0, 1, 2, 3, 4, 5, 6. Plan: 5-fold cross-validation, shuffled with seed 1 → 5 fits per candidate (p × k = 1 × 5), 35 in total. … 5-fold cross-validation picks degree 3 (score 0.0768); its honest test MSE is 0.0889. Picking by test error would report degree 4 at 0.0868 — a leaked, optimistic figure.
Simplified: One fixed toy dataset (20 development + 10 test points from sin(2πx) with noise) and polynomial least squares of degree 0–6 as the candidate models. Shuffling uses a seeded mulberry32 Fisher–Yates; folds are contiguous chunks like scikit-learn's KFold. Hold-out uses the first of k chunks as the validation set.
Loading the simulation…
Why Trust a Single Score
Suppose you have a labeled dataset and want to know how well a model will handle new data. The most common first step is to divide the dataset once, fit the model on one portion, and score it on the portion that was held back. That single score feels like a clean answer. This article asks a sharper question: how much should you trust that one number, and what does a different evaluation strategy buy you? Cross-validation is the answer most practitioners reach for, and its advantages over a single split fall into three groups that we will take in turn.
We will follow the procedure step by step, watching how the held-out portion of the data changes from round to round and how the collection of scores changes as a result. Keep one question in mind throughout: what is the score actually measuring?
How One Random Draw Misleads
A single train-test split is produced by random selection. That randomness is harmless on average, but any one particular draw can be favorable or unfavorable. Think about what happens when the examples that are hardest to classify all land in the training portion. The held-out portion then contains only the comparatively easy cases, and the score you measure looks better than the model deserves. Now reverse the situation: if all the hard cases land in the held-out portion, the score looks worse than the model deserves. In both cases the number is a product of the luck of the draw as much as the quality of the model.
Imagine a small labeled dataset of 10 examples, where 3 are genuinely difficult. If a single random split happens to place all 3 difficult examples in the training portion, the held-out examples are mostly easy, and the reported score is inflated. If the 3 difficult examples instead land in the held-out portion, the same model reports a deflated score. The model did not change between the two cases; only the draw did.
Giving Every Example a Turn
Cross-validation attacks the problem of luck by changing the structure of the evaluation rather than hoping one split is representative. The dataset is partitioned into a number of folds of roughly equal size. In each iteration, one fold is set aside as the test set and the model is fitted on the remaining folds. The procedure repeats until every fold has served as the test set exactly once. Because each example belongs to exactly one fold, each example is evaluated exactly once. No record can escape evaluation through an unlucky or convenient draw.
Notice what the diagram makes visible. The training portion changes in every round because a different fold is removed from it, while the test portion always rotates to a fold that has not yet been held out. After five rounds, the held-out folds cover the whole dataset with no overlap.
Tracing Ten Examples Through Five Folds
Five folds over ten labeled examples
A dataset has 10 labeled examples named A through J. Split it into 5 folds of 2 examples each and track which examples are tested in each round.
- 1Partition the data
Fold 1 is {A, B}, fold 2 is {C, D}, fold 3 is {E, F}, fold 4 is {G, H}, and fold 5 is {I, J}. Each example belongs to exactly one fold.
- 2Round 1 test set
Fold 1 {A, B} is the test set. The model is fitted on the other 8 examples, C through J.
- 3Round 2 test set
Fold 2 {C, D} is the test set. The model is fitted on A, B, and E through J.
- 4Continue the rotation
Folds 3, 4, and 5 each take a turn as the test set, and the remaining folds form the training data each time.
- 5Check coverage
Across the five rounds, A through J are each tested exactly once, so all 10 examples are evaluated.
Five rounds produce five scores. Each example contributes to exactly one of them, and each round trains on 8 of the 10 examples.
Notice that the held-out examples are never shared between rounds. That is what makes the evaluation complete: no single lucky draw can decide the outcome, because every example contributes its turn in the test set.
Averaging Across Folds
This changes what a good score means. Each cross-validation score comes from a different held-out fold, so the model has to perform well on every portion of the data, not just on one favored portion. When the individual scores are consistently high, and their mean is high, you have evidence that the model generalizes across the dataset rather than merely fitting one particular split.
The left column shows why a single split is fragile: whichever draw you happen to get determines the answer. The right column shows the alternative. Each fold contributes one score, and the mean of those scores is far less dependent on any one draw, because an extreme draw in one round is balanced by ordinary draws in the others.
Reading the Spread of Scores
Having several evaluations does more than produce a steadier average. The collection of scores also reveals how sensitive the model is to which examples it is trained on. If every fold gives nearly the same score, the model's performance appears stable across these training choices. If the scores vary widely, the model's performance depends noticeably on the particular training data it receives.
A concrete case from the source makes this clear. For the iris dataset, the cross-validation accuracies ranged between 90% and 100%. That is a wide band. Read as a single number, the performance could look excellent or merely acceptable depending on which fold you looked at. Read as a range, the band tells you something useful: it gives an idea of how the model might perform in a worst-case and a best-case scenario when it meets new data. A single split would have hidden this entire picture behind one value.
Calculating the Training Share
The third advantage concerns how much of the dataset actually shapes the model. A common single split reserves about a quarter of the data for evaluation, so roughly 75% of the examples are used to fit the model. Cross-validation changes that proportion. In k-fold cross-validation, each iteration fits the model on all folds except one, so the training share is the remaining $k-1$ folds out of $k$ total folds.
What do you think happens?
With five folds, what fraction of the data is used to fit the model in each round?
Reveal answer
$\frac{4}{5}$, which is 80% of the data.
Here k is 5, so k minus 1 is 4 folds used for training out of 5 total folds, giving $\frac{4}{5}$.
Comparing five-fold and ten-fold training shares
Find the training share for k = 5 and for k = 10, and compare both with a typical single split.
- 1Substitute k = 5
The training share is $\frac{5-1}{5} = \frac{4}{5}$, which is 80%.
- 2Substitute k = 10
The training share is $\frac{10-1}{10} = \frac{9}{10}$, which is 90%.
- 3Compare with a single split
A typical single split uses about 75% for training, so five-fold uses more data for fitting and ten-fold uses more still.
Five-fold fits on 80% of the data and ten-fold fits on 90%. As k grows, each training share moves closer to the full dataset.
The source also notes the practical reason this matters: more data will usually result in more accurate models, so a larger training share is often worth having. Larger k therefore buys more training data per round, but it also means more rounds to run, so the choice of k is a trade-off rather than a rule that bigger is always better.
Mistakes Students Make
Common mistake Reporting only the mean of the fold scores
Why it failsThe mean hides how much the scores vary. Two models with the same mean can differ greatly in sensitivity to the training data.
Do this insteadReport the range or spread of the fold scores alongside the mean.
Common mistake Assuming the held-out fold is the same in every round
What it looks likeUsing fold 1 as the test set in all five rounds.
Why it failsThen most examples are never evaluated, and the procedure loses its purpose of giving every example a turn.
Do this insteadRotate the held-out fold so that each fold serves as the test set exactly once.
Common mistake Confusing the training share with k
What it looks likeBelieving five-fold cross-validation trains on 5 folds.
Why it failsIn each round the model is fitted on all folds except one, so five-fold trains on 4 of 5 folds.
Do this insteadUse $\frac{k-1}{k}$ to find the training share, which is $\frac{4}{5}$ for five folds.
Common mistake Treating one split's score as a precise measure of model quality
Practice Choosing k
A team runs ten-fold cross-validation on a model and records fold accuracies ranging from 70% to 95%. Without changing the model, they switch to five-fold cross-validation and get fold accuracies ranging from 85% to 90%. Using the ideas in this article, explain what the narrower range suggests about the two evaluations, and calculate the training share for each setting.
Hints
- Apply the formula $\frac{k-1}{k}$ separately for k = 10 and k = 5.
- Think about what a wide range says about sensitivity to the training data.
- Consider whether the narrower range proves the model is better or only that these particular folds agreed more closely.
Key Takeaways
- A single train-test split depends on one random draw, so a lucky or unlucky split can make a score look unrealistically high or low.
- In k-fold cross-validation, each fold serves as the test set exactly once, so every example is evaluated exactly once and the model must generalize across the whole dataset.
- The spread of fold scores reveals sensitivity to training data: a narrow range suggests stable performance, while a wide range such as the iris 90% to 100% band suggests dependence on which examples were used for training.
- The training share in k-fold cross-validation is $\frac{k-1}{k}$, which is 80% for five folds and 90% for ten folds, compared with about 75% for a typical single split.