Introduction: Honest Evaluation
The fundamental goal of machine learning isn't to get a good score on the training data. It's generalization: your model must perform well on data it has never seen, real-world data it will encounter in production.
If you evaluate your model on the data it used to learn, you're lying to yourself. It's like giving a student the answers to an exam so they can memorize them, then giving them the same exam: they'll score 100%, but they haven't understood anything. In machine learning, a model can memorize its training data (that's overfitting) and appear perfect, but collapse when faced with new data.
The solution is simple in principle: set aside data that the model will never see during training, and use it for an honest evaluation.
A rigorous split without data leakage is what separates a research model from a deployable model. No metric is reliable without it.
The Three Sacred Sets
The training set (70-80%)
This is the bulk of the dataset, the data on which the model actually learns. At each training step, the network's weights are adjusted to minimize the error on this data. The model revisits it dozens or even hundreds of times, each complete pass is called an epoch.
The validation set (10-15%)
The validation data is your dashboard during development. You never train directly on it, but you use it to make decisions:
- Choosing hyperparameters, the settings you fix yourself, as opposed to the parameters training discovers: which learning rate, which regularization strength gives the best validation results?
- Selecting the best architecture: a 3-layer vs 5-layer model, which generalizes better?
- Deciding when to stop training (early stopping): when performance on validation starts degrading, that's a sign of overfitting
The model never sees this data during weight learning. But be careful: by making decisions based on validation, you're indirectly "consuming" information from this set.
The test set (10-15%)
The test set is sacred. You touch it only once, at the very end, when all your decisions are made. It's your honest, unbiased estimate of the model's real performance. If you use the test set to compare architectures or tune hyperparameters, it loses its value: it's no longer a reliable estimate, it becomes a second validation set.
Why Three Sets and Not Two?
If you use the same set to choose hyperparameters AND estimate final performance, you introduce an insidious optimistic bias.
Here's why: imagine you test 100 different hyperparameter combinations and keep the one that gives the best score on your evaluation set. Among these 100 combinations, some will "score well" by pure chance (statistical fluctuations). You'll naturally select one of them, and your performance estimate will be inflated. It's a well-known statistical phenomenon: when you select the best result among N trials, you also select the most favorable random fluctuation.
The test set, preserved intact and never used for any decision, gives an unbiased estimate of true performance. The gap between validation performance and test performance reveals exactly the magnitude of this optimistic bias.
Typical Ratios
Plenty of data (over 100,000 examples)
90/5/5 or 98/1/1. Even 1% is thousands of examples. Enough for evaluation.
A moderate amount (1,000 to 100,000)
70/15/15 or 80/10/10. The balanced classic.
Little data (under 1,000)
Forget fixed ratios. Use cross-validation (see below) to use all your data.
Fix the random generator's seed, the random_state argument in the usual libraries. Without it, every run produces a different split, and you can no longer reproduce your own results or compare two attempts. Reproducibility is the bare minimum.
Cross-Validation: Using 100% of the Data
When you have little data (a few hundred examples), a fixed 70/15/15 split wastes precious data: 30% of your data never serves for training. K-fold cross-validation solves this problem by using 100% of the data for both training and evaluation.
Algorithm:
- Divide the data into equal parts. Each part is called a fold
- For each fold : train the model on the other folds, evaluate on fold
- Final performance = mean (and standard deviation) of the k results
Common values: k=5 or k=10. With k=5, each fold contains 20% of the data, and the model is trained on 80% at each iteration. It's a good trade-off between estimation reliability and computational cost.
Advantage: each data point is used exactly k-1 times for training and exactly 1 time for evaluation. Nothing is wasted, and the performance estimate is more reliable because it's averaged over k independent evaluations.
Disadvantage: you train k models instead of one, so it's k times slower. For lightweight models, it's negligible; for large neural networks, it can become prohibitive.
Example with k=5 and 1,000 data points:
| Fold | Training | Evaluation |
|---|---|---|
| 1 | 800 examples | 200 examples |
| 2 | 800 examples (different) | 200 examples |
| 3 | 800 examples (different) | 200 examples |
| 4 | 800 examples (different) | 200 examples |
| 5 | 800 examples (different) | 200 examples |
Performance = average of the 5 results
Stratified Splits: Preserving Proportions
In classification with imbalanced classes, a naive random split can create catastrophic situations. Imagine a dataset with 95% negative cases and 5% positive cases: an unlucky split could create a validation fold with 0% positive cases. Your evaluation would be completely skewed.
A stratified split guarantees that each fold (or each train/val/test set) maintains exactly the same class proportions as the overall dataset.
For example, if 5% of your data is the rare class "fraud," each fold will also contain approximately 5% fraud. This makes evaluations comparable across folds and avoids statistical surprises.
The usual libraries offer stratified variants of every splitting scheme (StratifiedKFold and equivalents). It costs nothing in compute and makes evaluations markedly more stable.
Time-ordered data: respecting chronology
For temporal data (stock prices, weather, server logs, monthly sales), a random split isn't just suboptimal, it's a major bug that can go completely unnoticed.
The problem: by randomly mixing dates, the model trains on March 2025 data to predict January 2025. It's literally using the future to predict the past. In production, it will never have access to the future, so its performance will collapse. But in dev, the metrics will be excellent, making the bug invisible and dangerous.
The Time Series Split respects chronology: the model always trains on the past and is evaluated on the future, exactly like in production.
| Fold | Training | Validation |
|---|---|---|
| 1 | Months 1-3 | Month 4 |
| 2 | Months 1-4 | Month 5 |
| 3 | Months 1-5 | Month 6 |
| ... | ... | ... |
This is the only fair way.
Data leakage: the silent error
Data leakage occurs when information from the test set or validation set "leaks" into the training process, in one way or another. The model gets an unfair advantage: it has access, directly or indirectly, to information it shouldn't know.
The classic symptom: excellent performance in development that collapses in production. The worst part is that it's invisible to the naked eye. All your metrics are beautiful, your learning curves are perfect, and nothing tips you off. Leakage can survive months in production before anyone realizes that the model doesn't generalize at all.
Your test metrics are excellent. Everything works perfectly... until you deploy. Then, collapse. The leakage has sabotaged all your work.
Common Types of Leakage
Direct leakage
A feature directly contains the target information. Example: predicting whether someone will be hospitalized using a feature "hospital room number" (which only exists for hospitalized patients).
Temporal leakage
Using the future to predict the past. Random splits on temporal data. Features based on the "event date" you're trying to predict.
Leakage through preprocessing
Computing normalization (mean/variance) on train + test, then normalizing. The test set indirectly influences training through the statistics.
Correct: compute stats on train ONLY, then use them to normalize both train AND test.
Leakage through duplication
The same data (or near-identical data) present in both train and test. This is common in medical imaging (multiple slices from the same patient) or in NLP (Natural Language Processing) with paraphrases of the same text.
Leakage through groups
Related data (from the same patient, the same user) split between train and test. The model recognizes the group instead of the pattern you're looking for.
The fix is to split by group rather than by example: all of one patient's data stays together in the same fold. Libraries provide this under names like GroupKFold.
Prevention: The Checklist
1. Split First, Then Preprocess
The split must be the very first step. Normalization, imputation, feature engineering: everything on the training set ONLY, then apply to the others.
2. Use Pipelines
The pipeline mechanisms in the usual libraries (scikit-learn and others) chain preprocessing and model into a single object, which guarantees the preprocessing is fitted on the training set and only applied to the rest. It is the most reliable protection, because it does not rely on your vigilance.
3. Look for Suspicious Features
A near-perfect correlation with the target? Likely leakage. Examine its origin.
4. Respect Groups
If examples are related, keep them on the same side of the split.
5. Validate Under Real Conditions
Simulate production: the model only has access to data available at the time of prediction.
Anti-Leakage Checklist
Before claiming victory:
- Split before any preprocessing
- No feature uses future data or test data
- Related examples are in the same split
- Normalization statistics come ONLY from train
- Consistent performance between validation and test (suspicious gap = red alert)
- Important features are plausible in production
- Production performance matches test metrics
Why This Matters
A rigorous split isn't a technical detail you can neglect. It's the foundation on which your entire evaluation rests. Without a clean split and without data leakage, no metric is reliable, no model comparison is valid, and no business decision based on these results is justifiable. Data leakage is particularly insidious because it's invisible: nothing in the numbers tells you something is wrong. You build blindly, convinced you have an excellent model, until you deploy in production and reality catches up. Taking these precautions from the start of a project is the difference between serious data science work and an illusion.
1. Why must the test set be used only once, at the very end?
2. You are predicting monthly sales and you split your dated data at random. The development metrics look excellent. Where is the danger?
3. You compute the mean and standard deviation over the full dataset, normalize everything, then split into train and test. What is the problem?
4. In medical imaging, several slices from the same patient end up some in the training set, others in the test set. Consequence?
5. You only have 600 examples. Why prefer cross-validation to a fixed 70/15/15 split?