Splitting data: train, validation and test
Why we hold data back, how much, and how to split when rows are not independent.
Lesson 11 of 6610 minBeginner
You will learn to
- Explain the role of train, validation and test sets
- Choose a split size
- Pick stratified, grouped or time-ordered splits
Before this lesson
Intuition
A student who practises with last year's exam and is then graded on the same exam proves nothing. To know whether someone has learned the subject, you grade them on questions they have not seen. Machine learning follows the same logic: a model must be scored on rows it did not learn from.
That leads to three roles for data:
- Training set: the rows the model learns from.
- Validation set: rows used to compare options, such as algorithms or hyperparameters. You look at these scores repeatedly while you make choices.
- Test set: rows used once, at the end, for the final estimate.
Why separate validation and test? Because every time you choose something based on a score, you tune towards that data a little. After many choices the validation score is flattering. The test set has taken no part in any choice, so it is the one number you can quote without apology.
In practice you rarely carve out a fixed validation set. You hold out the test set, then cut the training part into folds and rotate which fold plays the validation role. That is cross-validation.
Picture it
The math
Let there be rows and a training share . Then the training part has rows and the test part rows. With and , that is 700 and 300.
The test score is an estimate, and its precision depends on the size of the test part. For accuracy measured on independent test rows, the standard error is approximately
and an approximate 95% interval is . A bigger test part gives a tighter estimate, but takes rows away from training. The choice of balances two errors: a model that is too weak because it saw too little data, and a score too noisy because it was measured on too little.
For a rare class, the random split itself adds noise. If a fraction of the rows are positive and the test part has rows, a random split gives a number of test positives with mean and a spread that stratification removes.
Worked example
We have 1,000 rows, 5% of them positive (50 rows), and a true accuracy of about 0.80.
Choosing the size. Using the formula with :
| Test rows | Standard error | 95% interval half-width |
|---|---|---|
| 100 | 0.040 | 0.078 |
| 300 | 0.023 | 0.045 |
| 600 | 0.016 | 0.032 |
With 100 test rows, a measured 0.80 could easily mean anything between about 0.72 and 0.88. With 300 rows the interval is about half as wide. This is why tiny test sets give unconvincing numbers.
Stratifying. With a plain random 70/30 split, the test part (300 rows) expects 15 positives, but the standard deviation of that count is about 3.2 (computed from the hypergeometric distribution), so 12 or 18 positives are normal. A stratified split puts 35 positives in training and 15 in test, every time. With 15 positives, recall will still be a coarse estimate (each missed case moves it by almost 7 points), which is a reason to prefer cross-validation for small or imbalanced data.
Groups and time. If rows belong to patients, stores or users, split by group so that no group has rows on both sides. If rows are ordered in time and you forecast the future, train on the earlier part and test on the later part. A random split in these cases lets the model see the answer's neighbours, as explained in Data leakage.
How mlflow does it
- Default hold-out. By default 70% of the rows are used for training and 30% are held out as a test set. In the quick train wizard, the Train / test split slider changes this and shows the row counts on each side.
- Stratified for classification. For classification the hold-out is stratified on the target, so class shares match on both sides. Regression splits at random.
- Group column. With a Group column chosen in the Cross-validation card, every group stays whole on one side, and the training share counts groups instead of rows. You need at least as many groups as folds.
- Folds inside the training part. Algorithms are compared with cross-validation on the training part only (CV folds from 2 to 20, default 10), so the held-out rows are not used to choose a model.
- Time series. Forecasting pipelines use Forecast settings with a date column, a forecast horizon and expanding or sliding folds, so validation always comes after training.
Common mistakes
- Splitting after preprocessing. Statistics learned on all rows leak test information. Split first; use pipeline steps for everything else.
- Reading the test score many times. If you try ten ideas and keep the one that scores best on the test set, the test set is now a validation set and its score is optimistic.
- A test set too small to mean anything. With a few dozen rows, accept wide uncertainty or use cross-validation.
- Random splits for grouped or time-ordered data. Use group or time-ordered splits whenever the model will meet new groups or the future.
- Forgetting the rare class. Without stratification, a rare class can be nearly missing from the test part.
- Splitting off the test set, then adding it back for tuning. Retraining on all rows at the very end is fine once the choices are made, but never use the test rows to make those choices.
Try it in mlflow
Open the cross-validation settings
- Open Pipelines and pick a classification pipeline.
- Open the Models part and find the Cross-validation card.
- Look at CV folds and Fold strategy: these decide how the training part is split for validation.
- In the quick train wizard, move the Train / test split slider and see how many rows go to each side.