Train/Test Split
Train/Test Split — Hold out data to measure real generalization.
If you grade a model on the same data it studied, of course it aces the test. A train/test split holds out unseen data so you measure how well the model actually generalizes.
Drag the divider to set the train / test split (17 train · 11 test)
- Train
- Test
- Model
- Not dealt yet
- Train error
- Test error
Split controls
The idea in plain words
If you grade a model on the same data it studied, of course it aces the test. A train/test split holds out unseen data so you can measure how well the model actually generalizes — the honest number that overfitting would otherwise hide.
The catch is leakage: accidentally letting test data into training makes the test score a lie. And if the training set is tiny, the fit becomes unstable and changes every reshuffle.
Now, the math
Generalization is judged on the held-out test set, not the training set:
- the test set — never touched during fitting.
- the model’s prediction, trained only on the train split.
▸ Show the derivation
A fair test error estimates performance on future data only if the test set is truly independent. Leakage (fitting on test points, or letting information cross over) collapses that gap and yields an over-optimistic score that won’t hold up in production.
Trace it by hand
Ten targets y = 2, 4, 4, 6, 6, 8, 8, 10, 10, 12 and the simplest model there is: predict the mean of the training set. We hold out 2 of the 10 points (an 80/20 split) and compare an ordered split against a shuffled one. All arithmetic is exact.
Fit on the training split
Ordered split: the test set T is the last two points, y equals 10 and 12. The model never sees them.
Score on the training set
Score on the held-out test set
More than four times the training error — holding out the two largest values made the split unrepresentative.
Shuffle before splitting
A shuffled test set, y equals 4 and 10, spans the range instead of sitting at one end.
What just happened: Same ten points, same model — but the ordered split reported a test error of 26 while a shuffled split reported 9 (against a training error of 6). Which points land in the test set changes the verdict, which is why splits are shuffled and why cross-validation averages several of them.
Now Break It
Try this: Leaking test data into training (or too small a test set) makes the score meaningless.
Control: Test-set size slider (set to near zero)
What happens: Unreliable estimate! A tiny test set gives a noisy score you can’t trust.
Where train/test split is used
A train/test split measures how well a model generalizes to data it has never seen. You train on one portion and evaluate on a held-out portion that the model never touched during fitting, giving an honest estimate of real-world performance. This directly exposes overfitting: a model that memorizes noise scores near-perfectly on training data but poorly on the test set. For time series such as stock prices or demand forecasting, a random split is wrong because it lets the model peek at the future to predict the past; instead you split chronologically, training on earlier data and testing on later data. For imbalanced problems like fraud detection, stratified splitting preserves the rare-class proportion in both halves.
A subtle but serious pitfall is tuning hyperparameters against the test set. Every time you look at the test score and adjust the model, information leaks and the estimate becomes optimistic; the test set should be touched once, at the very end. The fix is a three-way split into train, validation, and test, or cross-validation on the training portion. Another common error is fitting any preprocessing, such as scalers, imputers, or feature selectors, on the combined data before splitting, which contaminates the test set. Grouped data also needs care: if the same patient or user appears in both splits, the model can cheat by recognizing the individual rather than learning the underlying pattern.
Frequently asked questions
What is a good train/test split ratio?
Why do I need a validation set if I already have a test set?
How should I split time series data?
What is stratified splitting and when do I need it?
Should I set a random seed when splitting?
Written & reviewed by the ML Visualization team · Last updated .