Cross-validation
How to find out how well a model will do on data it has never seen: hold rows back, rotate which rows are held back, and average. Worked through on 100 examples and five folds.
In 30 seconds
A model must be judged on data it did not learn from. Holding back one slice works, but the score then depends on which slice you happened to pick.
k-fold cross-validation cuts the data into k parts, trains k times, each time holding out a different part for scoring, and averages the k scores.
Validation scores are for choosing between models. A separate test set, locked away and used once, gives the final honest number. Both are ruined by leakage: any information from held-out rows reaching training.
Cross-validation, step by step
All scores here are made up to show the procedure. Plain k-fold assumes rows are independent; time series and grouped data (several rows per person) need splits that respect time or groups.
- Step 1: Marking your own homework. A model that is tested on the very rows it learned from can simply remember the answers. The 99% says how well it memorised, not how it will do on data it has never seen, which is the only thing we care about.
- Step 2: One split, one opinion. The fix is to hold some rows back: train on 80, test on the other 20. But the score now depends on which 20 were held back. One split gives 90%, another gives 78%, and with a single split there is no way to tell which is closer to the truth.
- Step 3: Cut the data into k folds. Shuffle the rows, then cut them into k equal parts called folds; here k = 5. In round 1 the model trains on folds 2 to 5 and is scored on fold 1, which it has not seen. That held-out fold is the validation fold. Round 1 scores 82%.
- Step 4: Rotate the held-out fold. Repeat with a fresh model each round, holding out a different fold every time. After five rounds every row has been used for validation exactly once and for training four times, so no row is wasted and no single lucky split decides the result.
- Step 5: Average the rounds. The cross-validation score is the average of the five rounds: 84%. The spread matters too. Scores from 78% to 90% show how far a single split could have misled us, and how much to trust a small difference between two models.
- Step 6: Lock away a test set. Three roles. Training rows teach the model. Validation folds compare models and settings. Because the winner was chosen for its validation score, that score flatters it slightly, so a test set is locked away before any work starts and used once, for the final number.
- Step 7: Data leakage. Leakage is information about held-out rows slipping into training. A classic case: choosing which input columns to keep by looking at all rows before splitting, so the validation fold has already shaped the model. Others: the same person on both sides of a split, or tuning against the test set. Scores then look too good.
Try it yourself
Learn this first
Learn next
- Precision, recall and F1Why 92% accuracy can hide a filter that misses half the spam, and what to measure instead. Beginner
- Confusion matrixCount every prediction by what it was and what the model called it. Beginner
- Support vector machinesFind the widest street between two groups of points, then bend it with a kernel. Intermediate
Spotted a mistake on this page?