K-Fold Partitioning & Generalization
One test split is one lottery ticket. K-fold makes every data point take a turn as the examiner — watch the holdout rotate forever, then drive the machine yourself.
Five Splits, One Honest Score
A single train/test split suffers from sample variance: a lucky split makes a weak model look brilliant; an unlucky one buries a strong model. The fix is to hold a tournament:
- 1. Chop the dataset into equal folds.
- 2. Train on folds, validate on the one left out.
- 3. Rotate the holdout fold until every fold has been the examiner once.
- 4. Average the scores — now every point was validated exactly once, by a model that never saw it.
In the loop on the right, watch the orange validation fold march down the diagonal. Each flash is a fresh model trained from scratch — nothing about the model carries over between rounds, which is exactly why the average is trustworthy.
Change K, Watch the Variance
Switch between . Larger means more training data per round (less biased estimate) but higher variance between folds and more compute. The trade-off ends at Leave-One-Out (): maximal data, maximal variance, maximal cost.
Variants, Formulas & the 1-SE Rule
Preserves class ratios inside every fold — the default choice for imbalanced data. Without it, a fold may contain almost no positives and report nonsense.
If data is ordered (by time, by class, by source), unshuffled folds become systematically different. Always shuffle — unless order is information, as in time series.
Tuning hyperparameters on the same CV you report is leakage. Nested CV runs an inner loop for tuning inside an outer loop for honest evaluation.
The overall cross-validation generalization score aggregates the out-of-fold validation loss across all splits:
The One-Standard-Error Rule: when comparing models, don't blindly take the minimum. Pick the simplest model whose error sits within one standard error of the best — it will generalize more reliably:
This estimator feeds every experiment in Topics 06–09: the tree depths, forest sizes, and network capacities ahead are all chosen by exactly this loop.
Leave One Out, or K = N
Put the single hold-out set back under the lamp. One partition, one trained model, one number — and that number is only as stable as the split. Move a handful of rows between train and validation and the measured error can swing by more than the real gap between two competing models. It is a high-variance estimate because it is built from a single draw.
Leave-One-Out Cross-Validation pushes the dial to its limit: set , one fold per observation. Each round trains on points and validates on the single point left out. Rotate until every point has had a turn as the examiner, then average the scores. Nothing is held back — every model sees almost the entire dataset.
The appeal is genuine: the estimate is nearly unbiased. Each training set is the full dataset minus one point, so LOOCV scores the model you would actually ship, not one starved of 20% of the data. But the validation scores are not independent. Consecutive models differ by a single training point, so their errors move together. Correlated scores do not average down — the standard error barely shrinks — and the estimate stays noisy. You bought that noise with full fits.
That is why nobody ships LOOCV for large : rounds at the compute of one fit, for an estimate no steadier than a cheap would give. It survives only where the arithmetic changes — tiny datasets where is small, and models such as linear or ridge regression where every leave-one-out residual can be recovered from a single fit.
…and so on for all N rounds. Every trained model sees N − 1 points; every score rests on exactly one.
Choosing K
is a dial with a failure mode at each end. Turn it down and every training set is small: the model you score is systematically worse than the one you will actually ship, so the estimate reads pessimistic and lurches between folds. Turn it up and each training set approaches the full dataset: the estimate turns optimistic and stable, but you paid for fits to measure a model that is already almost the shipped one.
Plot measured error against and you get a rough U: bias falls monotonically as training sets grow, variance climbs as the rounds become near-copies of one another, and the sum dips in between. The useful choice is the flat bottom of that U, not either wall.
Between roughly 5 and 10 the bottom is flat enough that moving stops paying. Five folds train on 80% of the data and cost five fits; ten folds train on 90% and cost ten. Past ten the estimate barely moves and you are mostly buying arithmetic — which is why and are the defaults in essentially every library, tutorial and paper.
Random folds can starve a fold of the minority class: with a 2% positive rate, some folds land near zero positives and report a meaningless score. Stratified K-fold samples each fold so its class ratio mirrors the dataset. Reach for it whenever the target is imbalanced.
Rows are not always independent. Ten scans from one patient scattered by a random split means the model trains on nine and is validated on the tenth — a near-copy it has already memorized. Grouped K-fold keeps every group wholly on one side, so the score reflects unseen patients, not unseen rows.
Ordered data must never be shuffled: validating on Tuesday with a model trained through Wednesday leaks the future backwards into the past. Rolling and expanding-window splits always train on what came before and validate on what comes next, fold by fold.
