Visual explainer
Cross-Validation
Rotate five held-out folds, average their errors, and keep preprocessing and the final test set outside the wrong training boundary.
Cross-validation checks a learning procedure on several held-out slices instead of relying on one split. Each round fits a fresh model; averaging its validation errors gives an estimate, not the true population error. Keep a separate test set for final evaluation.
Both strips contain one hundred development rows: eighty for training and twenty held out. B and C name different held-out subsets, so the training rows also change. In our synthetic classification example, B has two incorrect predictions out of twenty, or 10%; C has five, or 25%. These are stipulated results to make the arithmetic visible, not measured model performance. Neither split tells you the unknown population error.
Partition the Development Rows
A fold is a subset of rows. A–E each contain twenty different rows, with no overlap. Their lengths encode equal membership counts: 5 × 20 = 100. C's outline identifies a fold, not a harder group. The additional twenty-five final test rows are outside this strip.
Here we assume independent rows drawn from the population we want to predict. The horizontal order identifies sets, not dates. Random shuffling can suit exchangeable rows; forecasting needs splits that respect time, and repeated observations of one person may need group-aware splits.
Rotate and Refit
Columns keep the same A–E membership. Each row is a fresh fit of the same learning procedure, with V marking validation and the other four cells training. Fit 1 learns from B–E and predicts A; fit 2 learns from A, C, D and E and predicts B. Continue through fit 5.
Every development row contributes to four training sets and receives one prediction from a model fitted without its fold. Do not continue fit 1's learned weights into fit 2. Learned preprocessing also belongs inside each fresh fit. This is the basic k-fold procedure.
Preserve the Class Mix
Suppose ten of our hundred development labels are positive. Each dot is one positive row; each outlined fold also contains eighteen negative rows, aggregated rather than individually drawn. Ten positives divide evenly here: 10 / 5 = 2 per fold. The synthetic misses used below can all be among the negatives, so the counts remain consistent.
Stratification aims to preserve class proportions approximately; exact equality depends on the counts. It can prevent a validation fold with no examples of a rare class. It does not make related rows independent or remove uncertainty, and a more uniform class mix can hide some variability.
Average the Errors
Our stipulated miss counts for A–E are 4, 2, 5, 3, 4. Divide each by twenty to get 0.20, 0.10, 0.25, 0.15, 0.20. Bar lengths share a zero-to-30% error scale; shorter is better. The dashed reference and diamond mark the average:
(0.20 + 0.10 + 0.25 + 0.15 + 0.20) / 5 = 0.90 / 5 = 0.18.
Because the folds are equally sized, this also equals 18 incorrect / 100 out-of-fold predictions. Unequal fold sizes require weighting to recover a pooled per-row error. Other metrics, such as F1, need not equal their pooled counterpart even with equal folds. These are scores from five fitted models, not the score of an ensemble or the final refitted model.
Count the Fits
Each square is one model fit. With five folds, each fit uses eighty training rows and twenty validation rows. With ten equal folds on the same hundred rows, each uses ninety and ten. The outlined bars retain the hundred-row denominator; their empty tails are the held-out rows.
That costs five versus ten fits per candidate setting, plus one final refit after selection. Runtime also depends on training size and the model. More folds do not guarantee a more precise estimate. Choose a split strategy and compute budget for your task; a single holdout can be a practical compromise when repeated fitting is too expensive. The hyperparameter-tuning visual follows the candidate-selection step.
Fit Preprocessing Inside Each Fold
Zoom into a separate miniature feature example: the four training values are 0, 2, 4, 6; the two held-out values are 8, 10. Circles are training rows, diamonds held out. Both rulers use forty drawing units per feature unit. The training mean is (0 + 2 + 4 + 6) / 4 = 3.
Center both sets by subtracting that training-only mean. Training becomes −3, −1, 1, 3; validation becomes 5, 7. The points keep their spacing while the coordinate origin changes from raw zero to raw three. This drawing shows centering only, not division by a standard deviation. A pipeline can fit the transformation inside each fold, then apply it unchanged to that fold's validation rows.
Spread Is Descriptive
Return to the hundred-row classification fixture and the same zero-to-30% scale as the error bars. A and E both sit at 20%; E is displaced downward only to expose the duplicate value. The bracket spans the observed minimum 10% to maximum 25%, a range of fifteen percentage points. The diamond below marks the mean 18%.
This spread describes five scores. It is not a confidence interval for population error. Any two of these five training sets share sixty rows, so their fitted models and resulting scores are not independent replicates. Do not turn the range, or a naive standard-error calculation, into a claimed uncertainty bound. Stratified class balance does not fix this dependence.
Refit, Then Open the Test
After choosing the procedure and settings using development data, freeze those choices and fit the complete pipeline on all one hundred development rows. The smaller fold strip preserves membership; the model and test boxes are schematic containers, not proportional sample areas. The separate twenty-five test rows stay sealed until this final evaluation. The arrow to test means predicting and scoring, with no feedback into fitting.
The final model learns from more rows than each CV model did, so its test score need not equal 18%. If you tune using the CV scores, the winning CV score also reflects selection. Use the untouched test set, or nested cross-validation when evaluating the tuning procedure itself, rather than treating the winning validation score as an independent final assessment.
Where It Breaks
Return to the miniature feature fixture on the unchanged forty-unit scale. The dashed reference marks the correct training-only mean three. Let the two held-out diamonds feed the preprocessing fit, and the all-row mean becomes (0 + 2 + 4 + 6 + 8 + 10) / 6 = 5. The amber diamond and lower origin show the shift.
Leaky centering now produces training values −5, −3, −1, 1 and validation values 3, 5. A supposedly held-out feature distribution has influenced the learned transformation. Rotating the folds afterward cannot undo that information crossing. Fit transformations only on each round's training rows, as described in the preprocessing leakage guidance.
The example proves contamination of the fitted mean. Its downstream effect depends on the model and transformation; it does not establish any particular score inflation, and some models are invariant to centering. Correct preprocessing also cannot rescue a split that puts dependent subjects or future information on both sides. Cross-validation estimates performance under its split assumptions; it cannot guarantee those assumptions match deployment.
The Quick Version
- One holdout can score differently from another.
- Five disjoint folds cover every development row.
- Every round fits a fresh model without its validation fold.
- Stratification approximately preserves class proportions.
- Average equal-fold errors; this fixture gives eighteen percent.
- More folds cost more fits per candidate setting.
- Learn preprocessing only from each fold's training rows.
- Fold spread alone is not a confidence interval.
- Freeze choices, refit development data, then evaluate untouched test data.
- Leaky preprocessing and unsuitable splits can invalidate the estimate.