Topic 2 of 5 · 5 min read
Train, validation and test splits
Goal: Assign each step of a modelling workflow to the training, validation or test split and spot a workflow that peeks at the test set.
Split your data into three parts with three jobs: train the model on one, tune choices on the second, and judge the final model once on the third. A score is only honest when it is measured on data the model and its builder never used to make a decision.
Check yourself · 1 of 3
During model development, what is the validation split of a dataset used for?
Why it matters
A model can memorize its training rows. Its score on those rows tells you nothing about new data. You also make choices as you work: which features, which algorithm, how strong a regularization setting. Each choice you make by looking at a score quietly fits your choices to that data. After enough tries, even a held-out score is optimistic.
The three splits
- Training set: the model's parameters are fitted here. Largest part.
- Validation set: you compare models and settings here. You may look at it many times.
- Test set: touched once, at the end, to report the number you expect in production. If you look at it and then change the model, it has become a second validation set.
A common starting split is around 60 to 80 percent training, with the rest divided between validation and test. With little data, cross-validation (next topic) replaces the fixed validation set, but the test set stays untouched.
How to do it
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
X_train, X_val, y_train, y_val = train_test_split(
X_train, y_train, test_size=0.25, random_state=42, stratify=y_train
)
Do all preprocessing after the split: fit scalers, imputers and encoders on the training rows only, then apply them to the other two. If time matters (forecasting, churn by month), split by date, not at random, so the model never trains on the future.
Before and after
Before: scale all the data, try ten models, pick the one with the best test score, report that score. After: split first, fit the scaler on training only, compare the ten on validation, pick one, and report the test score once.
What goes wrong
- Test-set peeking. You check the test score, change a setting, check again. The reported number is now tuned to the test rows and is too high.
- Preprocessing leakage. Fitting a scaler on the full table lets test-set statistics shape the training data. The effect is small for scaling and large for target-based encodings.
- Duplicates across splits. The same customer or near-identical rows on both sides make the model look like it generalizes when it recalls. Split by group in that case.
- Random split on time data. The model sees next month's rows while training and then "predicts" last month.
Examples
Workflow review
| Step | Split it should use |
|---|---|
| Fit logistic regression weights | training |
| Compare C=0.1 against C=10 | validation |
| Choose between logistic regression and a random forest | validation |
| Report the expected accuracy to the product owner | test, once |
| Fit the standard scaler | training only |
A workflow that does "pick the C with the best test accuracy" has used the test set to tune and needs a fresh one.
Splitting a monthly dataset by time
A churn table covers January to December. Train on January to August, validate on September and October, test on November and December. Production will also predict forward in time, so this split matches how the model will be used. A random split would let the model learn from December rows when scoring March.
Key points
- Training data fits the model, validation data guides choices, and test data gives one final honest estimate.
- Every time you change the model after looking at a score, that data stops being a fair judge.
- Fit scalers, imputers and encoders on the training split only, after splitting.
- Split by date for time-ordered data and by group when one customer or document has many rows.
- The test set is used once; reusing it turns it into a second validation set.
Common mistakes
- Looking at the test score repeatedly while tuning, then reporting the best one.
- Scaling or imputing on the full dataset before splitting.
- Using a random split on time-ordered data, so the model trains on the future.
- Letting rows from the same customer land in both training and test.
Check yourself · 2 of 3
A colleague compares eight models and reports the best one's test accuracy. What is the problem?
Check yourself · 3 of 3
You fit a StandardScaler on the full table, then split into train and test. Why is this a leak?
Practice items, not counted towards your mastery.
Start here
- Machine Learning Crash Course (Google) (opens in a new tab)CourseFree
Free course covering features, labels, data splits, overfitting and regularization with exercises.
- Supervised learning (scikit-learn user guide) (opens in a new tab)DocsFree
The index of scikit-learn's supervised estimators, with the shared fit and predict interface.
- Cross-validation: evaluating estimator performance (scikit-learn) (opens in a new tab)DocsFree
Explains holding out a test set, k-fold, stratified folds, pipelines and data leakage with code.