Cross-Validation
Cross-Validation — Rotate the holdout set across k folds for a stable estimate.
A single train/test split can be lucky or unlucky. k-fold cross-validation rotates the holdout across k slices of the data and averages, giving a far more stable estimate of performance.
- Trained on
- Held out this fold
- Fold model
Data split into 5 folds — drag across the strip to add or remove folds
Validation error per fold (hover a bar to find its points)
Cross-validation controls
The idea in plain words
A single train/test split can be lucky or unlucky. k-fold cross-validation rotates the validation set across k slices of the data, scores each, and averages — a far more stable estimate of performance.
You watch the validation fold sweep and the per-fold scores accumulate into a mean ± spread. Too few folds (k = 2) gives a noisy estimate that swings run to run; more folds tighten it, at more compute.
Now, the math
The cross-validation score is the mean of the per-fold errors:
- the number of folds the data is split into.
- the error when fold f is the validation set.
▸ Show the derivation
Every point is used for validation exactly once and for training k−1 times, so the estimate uses all the data while never testing on training points. The spread across folds is itself informative — a large std warns that the score is sensitive to which data you held out.
Trace it by hand
The same ten targets y = 2, 4, 4, 6, 6, 8, 8, 10, 10, 12 with k = 5 folds: fold 1 holds out points 1-2, fold 2 points 3-4, and so on to fold 5 with points 9-10. The model again predicts the training mean, and E is the mean squared error on the held-out fold. Std rounded to 1 decimal.
Slice the data into 5 folds
Each fold takes a turn as the validation set while the other 8 points train the model.
Score fold 1
Folds 2 to 5 repeat the same recipe with their own training means of 7.5, 7, 6.5 and 6.
Collect all five fold errors
A single split could have reported anything from 1 to 26 for this same model and data.
Average the folds
Measure the spread
Squared deviations of each fold error from 13.5. The final report is 13.5 plus or minus 10.5.
What just happened: Depending on which two points were held out, a single split scored anywhere from 1 to 26. Five-fold CV distills that into 13.5 plus or minus 10.5 — a fairer average, with a spread that honestly warns how unstable the estimate is on only ten points.
Now Break It
Try this: Too few folds gives a noisy estimate; too many is slow and high-variance per fold.
Control: Number of folds slider
What happens: Noisy estimate! With too few folds the cross-validation score swings wildly run to run.
Where cross-validation is used
Cross-validation gives a more stable estimate of model performance than a single train/test split by rotating the held-out portion across the data. In k-fold cross-validation you divide the data into k equal parts, train on k minus one of them, and test on the remaining fold, repeating until every fold has served as the test set once. Averaging the k scores smooths out the luck of any one split, which is especially valuable on small or medium datasets where a single split can swing widely. It is the standard tool for hyperparameter tuning: grid and random search evaluate each candidate configuration with cross-validation so the chosen settings are not tuned to one arbitrary partition.
The most damaging cross-validation mistake is leakage from preprocessing done outside the loop. If you scale, impute, or select features on the full dataset and then cross-validate, each fold's test data has already influenced the transform, inflating scores. Wrap the entire preprocessing sequence in a pipeline so it is refit inside every fold. Data structure also dictates the scheme: for imbalanced targets use stratified k-fold, for grouped data such as repeated measurements per patient use grouped folds so no individual spans train and test, and for time series use forward-chaining splits rather than shuffled folds. Finally, more folds reduce bias but cost more compute; leave-one-out is thorough but can be noisy and expensive.
Frequently asked questions
How many folds should I use?
What is the difference between cross-validation and a train/test split?
What is leave-one-out cross-validation?
Do I still need a separate test set if I use cross-validation?
How do I cross-validate time series data?
Written & reviewed by the ML Visualization team · Last updated .