In plain words
To know if a model is any good, you must test it on examples it did not learn from. The usual way is to lock some data away as a test set. But with only one split, the score depends on which examples happened to land in the test pile. A lucky split flatters the model, an unlucky one is unfair to it.
Cross-validation fixes this by rotating. Split the data into, say, five equal parts, called folds. Train on four, test on the fifth. Then start again and let a different fold be the test, until every fold has had its turn.
You end up with five scores instead of one. Their average is a steadier estimate of how the model will do on new data, and how much they vary tells you how much to trust it. People use it to compare models and to pick settings such as the learning rate, without wasting precious data.
Everyday example
Five mock papers before the board exam
A student has five past papers. Judging herself on one paper she has already practised would prove nothing. So she studies from four and sits the fifth as a timed test, then repeats with each paper taking a turn as the exam. Her average across the five tests says more about exam day than any single score.
Where the example breaks: A student cannot forget the paper she sat last round. A model can: each round trains a fresh model from scratch on its four folds, so no test fold has ever been seen by the model being tested on it.
How it actually works
The most common form is k-fold cross-validation, with k usually 5 or 10.
Split into k folds
Shuffle the training data and divide it into k roughly equal folds. For classification with rare categories, stratified folds keep the same share of each category in every fold.
Train k times
In round i, train a new model on every fold except fold i, then measure it on fold i. Every example is used for testing exactly once and for training k − 1 times.
Average the scores
Report the mean and the spread of the k scores. A model scoring 0.98 with a spread of 0.02 is more convincing than one that swings between 0.80 and 0.99. Compare models or hyperparameter choices on this average.
Still keep a final test set
If you try many settings and keep the best cross-validation score, that score is slightly optimistic, because you chose it. So a separate test set is still held out for one last, honest check.
Avoid leaks
Any step that learns from data, such as scaling or picking features, must be fitted inside each training split only. For data ordered in time, use splits that always train on the past and test on the future, or the model gets to peek ahead.
- Typical choice
- 5 or 10 folds, generally preferred to leave-one-out
- Leave-one-out
- The extreme case: k equals the number of examples, each tested alone
- Cost
- Training runs k times, so it takes about k times as long as one split
- Influential paper
- M. Stone, "Cross-validatory choice and assessment of statistical predictions", 1974
Questions people ask
What is k-fold cross-validation?
Splitting the data into k equal parts and running k rounds of training and testing. Each round, a different part is the test, and the rest is the training data. The final result is the average score across the k rounds.
Why use cross-validation instead of a single train and test split?
A single split gives one score that depends on luck. Cross-validation uses every example for testing once, gives several scores to average, and works well when data is limited, at the cost of training several times.
What is the difference between a validation set and cross-validation?
A validation set is one fixed slice of data held back for tuning. Cross-validation rotates that held-back slice through the whole dataset, so you no longer need a fixed validation set. You still keep a final test set.
Does cross-validation prevent overfitting?
Not by itself. It detects overfitting by showing how a model does on data it did not train on, which helps you pick a simpler model or better settings. The fix comes from those choices.
Was this clear?
1 is confusing, 5 is crystal clear. One tap, no account needed.
Spotted something wrong, or know a better example? Suggest a change
Further reading
- Cross-validation: evaluating estimator performance (User Guide)scikit-learn
- Machine Learning Glossary: cross-validation, k-fold cross validationGoogle for Developers
- Cross-Validatory Choice and Assessment of Statistical Predictions (M. Stone)Journal of the Royal Statistical Society, Series B (Oxford Academic), 1974