Stratified k-Fold Cross-Validation and Other Strategies
Cross-validation evaluates how well a given algorithm will generalize on a specific dataset; it is not a method for building a model to apply to new data.
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 a Good Score Is Not Yet a Deployable Model
Cross-validation is one of the most common ways to check how well a learning algorithm is likely to perform on data it has never seen. It splits the available data into several parts, trains and tests repeatedly, and averages the results. Because it is so central to model evaluation, it is easy to slide into a belief that sounds reasonable but is wrong: that running cross-validation is a way of building the model you will eventually deploy. This article examines that belief, explains what cross-validation actually gives you, and then looks at its main practical drawback, which is cost.
Splitting Data into Folds
Cross-validation evaluates how well a given algorithm will generalize on a specific dataset. It is not a method for building a model to apply to new data.
The procedure begins by dividing the available data into k parts, called folds. In each round, one fold is held out as the test set and the remaining k minus 1 folds form the training set. A model is trained on the training folds and scored on the held-out fold. Then the next round holds out a different fold, and this continues until every fold has served as the test set exactly once.
Read the diagram as a rotation. The held-out slice moves one position each round, while the rest of the data serves as training material. Because every fold gets its turn as the test set, every observation contributes to evaluation exactly once, and no single lucky or unlucky split decides the outcome.
Watching the Rounds Repeat
Each round of cross-validation does build a model internally, since each fold needs a model trained on its training portion and scored on its held-out portion. The loop below shows that the same train-and-evaluate step runs once per fold, and each run produces one score.
Tracing a five-fold run on 100 observations
A dataset has 100 observations and cross-validation uses k equal to 5. How many folds are there, how many observations are in each fold, and how many models are trained during the run?
- 1Split into folds
With k equal to 5, the 100 observations are divided into 5 folds of 20 observations each.
- 2Assign the test role
Each of the 5 folds takes one turn as the held-out test set. In round 1, fold 1 is tested. In round 5, fold 5 is tested.
- 3Count the training runs
Each round trains one model on its training portion, which here is 80 observations. Five rounds therefore train five models.
- 4Collect the outputs
Each round produces one score. The five scores are averaged to estimate how well the algorithm generalizes on this dataset. No deployable model is produced by this process.
There are 5 folds of 20 observations each. The run trains 5 models and produces 5 scores, and the procedure returns those evaluation results rather than a model for new data.
What do you think happens?
If you run 5-fold cross-validation, how many separate models are trained?
Reveal answer
5
Each of the k rounds trains one model on its training portion, so five rounds train five models. A single split would train only one.
What Cross-Validation Hands Back
The purpose of cross-validation is to answer a question about an algorithm, not about a particular fitted model. The question is how well a given algorithm will generalize when it is trained on a specific dataset. The internal models built along the way are used only to produce scores. The procedure hands back the evaluation, and those models are not the output.
The Deployment Misconception
Suppose the scores look good. The natural next step is to decide whether to use the algorithm, and if so, to train a final model on the full dataset for deployment. Cross-validation supports that decision, but the deployable model still has to be produced by a separate training step. Treating the cross-validation output as if it were the finished product leads directly to the misconception.
Common mistake Deploying one of the fold models as the final model
What it looks likeAfter a 5-fold run, someone picks the model from round 3 because it scored best and ships it.
Why it failsThose fold models were trained on only part of the data and existed to produce scores. Cross-validation does not designate any of them as the product.
Do this insteadUse the scores to decide whether the algorithm is worth using, then train a final model on the full dataset in a separate step.
Common mistake Believing cross-validation is the way to build the model for new data
Why it failsThe purpose is to estimate how well an algorithm generalizes on this dataset, not to produce a fitted model.
Do this insteadTreat cross-validation as an evaluation procedure and keep model training as its own step.
Common mistake Assuming the averaged score is a guarantee for future performance on any dataset
The Cost of k Training Runs
The main disadvantage of cross-validation is increased computational cost. A single split of the data trains one model. Cross-validation trains k models instead, one for each of the k rounds, so the total work grows with k. The practical consequence is that cross-validation will be roughly k times slower than a single split of the data.
Here $T_{split}$ is the time to train and score one model on a single split, and $T_{CV}$ is the time for the full cross-validation run. The relation is a rule of thumb that follows directly from the fact that k models are trained where one model would otherwise be trained. The word roughly matters: real running times depend on the algorithm, the size of the data, and the hardware, so the factor of k is a planning estimate rather than an exact measurement.
Use the roughly k times estimate to plan how long a cross-validation run will take before starting it, then measure the actual runtime on your own data and hardware.
Stratified Folds and Other Variants
The title of this article names stratified k-fold cross-validation. It follows the same procedure described above: data is divided into k folds, each fold takes a turn as the held-out test set, and the scores are collected for evaluation. The stratified version differs in how the folds are formed. It aims to keep the mix of classes in each fold close to the mix in the full dataset, which a plain random split does not guarantee. The rotation, the repeated training, and the fact that the output is an evaluation rather than a deployable model all stay the same. The source material for this article does not detail other named variants, so they are not described here.
Diagnosing a Cross-Validation Report
A colleague runs 10-fold cross-validation on a dataset, gets ten scores, and then says, 'Great, the model is ready, so I will send the round-7 model to production.' Identify the two errors in this reasoning and describe what the correct next steps are.
Hints
- Ask what cross-validation returns: scores, or a model?
- Ask how many models the run trained and what each one was trained on.
- Think about whether a separate training step is needed on the full dataset.
Key Takeaways
- Cross-validation evaluates how well a given algorithm will generalize on a specific dataset; it is not a method for building the model you deploy.
- Each fold takes one turn as the held-out test set while the remaining folds train the model, and the process repeats until every fold has been tested once.
- Cross-validation returns evaluation scores, even though it builds models internally, so a deployable model must be produced by a separate training step.
- Because k models are trained instead of one, cross-validation is roughly k times slower than a single split; treat this as a planning estimate, not an exact runtime.