Train, Validation & Test Splits Explained (Why Testing on Train Data Lies)
The exact role of Training, Validation, and Test sets, choosing 80/10/10 vs. 60/20/20, Stratification, and why you must lock the Test vault until launch
Splitting your dataset divides historical rows into three strictly separate buckets: Training (to fit weights), Validation (to tune hyperparameters and compare models), and Test (locked in a vault for one final unbiased exam).
Evaluating a model on the exact data it memorized gives a fake 100% score — and repeatedly tuning on the test set silently overfits your choices to the test set too.
An honest Train/Validation/Test split is the foundation of the scientific method in Machine Learning — without it, you are flying blind.
Prevents shipping models that ace offline notebooks and crash on live user traffic.
Gives leadership an honest, unbiased forecast of real-world ROI before spending money on deployment.
Classic question: 'Why do we need THREE splits (Train/Val/Test) instead of just Train and Test?'
You have 10,000,000 rows of clickstream logs. Should you still hold out 20% (2,000,000 rows) for validation and 20% for test?
- Explain the distinct role of Training, Validation (Dev), and Test sets in plain English.
- Choose practical split ratios for small (1k rows), medium (50k rows), and massive (10M rows) datasets.
- Use Stratified splitting for imbalanced classification and Chronological splitting for time-ordered data.
- Avoid the #1 beginner mistake: tuning hyperparameters directly on the Test set.
If you hand a student the exact 50 final exam questions to study from, a 100% score proves memorization, not understanding. Machine Learning enforces honesty by keeping three separate rooms: Train (study), Validation (practice quizzes to pick your best model), and Test (the sealed final exam).
1. Practical Split Ratios by Dataset Size
| Dataset Size | Recommended Strategy | Why |
|---|---|---|
| Small () | 80% Train (with 5-Fold CV) + 20% Test | Too small for a separate single val split — K-Fold CV reuses training data smartly |
| Medium () | 70/15/15 or 80/10/10 | Plenty of validation & test rows for stable metrics |
| Massive () | 98/1/1 or 99/0.5/0.5 | 10,000–100,000 test rows already give tiny confidence intervals |
2. Three Split Rules You Must Never Break
- Imbalanced Classification → Stratified Split: Guarantees exact class proportions (e.g., 2% fraud) in Train, Val, and Test.
- Time-Ordered Data → Chronological Split: Train on Jan–Sep, Validate on Oct, Test on Nov–Dec — NEVER shuffle future rows into the past!
- Grouped Entities (Users/Patients) → Group Split: Keep all rows of the same user in ONE split so the model can't recognize the same person in both train and test.
Switch between a Leaky Pipeline (target-encoding & scaling on ALL rows before splitting) and a Clean Pipeline (fit ONLY on Train Fold):
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Yes! Once your sealed Test score has verified your chosen pipeline & hyperparameters, many teams retrain that exact frozen pipeline on Train + Val (or all data) before shipping to production — just never re-tune after looking at Test.
Compare Stratified K-Fold vs. Walk-Forward Time-Series Splitting
import numpy as np
# Stratified 70 / 15 / 15 Train-Val-Test Split From Scratch
def stratified_split(y: np.ndarray, val_frac=0.15, test_frac=0.15, seed=42):
rng = np.random.default_rng(seed)
tr, va, te = [], [], []
for cls in np.unique(y):
idx = rng.permutation(np.where(y == cls)[0])
n_te = max(1, int(round(len(idx) * test_frac)))
n_va = max(1, int(round(len(idx) * val_frac)))
te.extend(idx[:n_te])
va.extend(idx[n_te:n_te + n_va])
tr.extend(idx[n_te + n_va:])
return np.array(tr), np.array(va), np.array(te)
y = np.array([0] * 90 + [1] * 10) # 10% positive class
tr, va, te = stratified_split(y)
for name, idx in [("Train", tr), ("Val", va), ("Test", te)]:
print(f"{name:5s} | n={len(idx):2d} | positive_rate={y[idx].mean():.1%}")
train_test_split on stock prices, user retention, or medical scans with multiple visits per patient.