Sub topic 5
How much data goes where
This chapter assumes you can call train_test_split and know why a held-out test set exists. It covers how to choose the sizes of the training, validation and test parts.
Two costs pull in opposite directions
Every row you hold out is a row the model cannot learn from. Every row you keep for training makes your evaluation noisier. Split sizes balance these two costs.
- A small training set gives a weaker model, so the score you measure understates what you could have achieved with all the data.
- A small test set gives a noisy score. Two runs with different random splits can differ by several points, which makes model comparisons unreliable.
How noisy is a test score?
For accuracy, the standard error is approximately sqrt(p * (1 - p) / n), where p is the true accuracy and n is the number of test rows. The size of the training set does not appear in this formula. Only the test set size controls how precisely you measure.
import math
accuracy = 0.90
for n_test in (50, 200, 1000, 10000):
se = math.sqrt(accuracy * (1 - accuracy) / n_test)
print(f"n_test={n_test:>6} approx. 95% interval: +/-{1.96 * se:.3f}")Running this prints:
| Test rows | Approximate 95% interval around 90% accuracy |
|---|---|
| 50 | ±0.083 |
| 200 | ±0.042 |
| 1,000 | ±0.019 |
| 10,000 | ±0.006 |
With 50 test rows, a measured 90% could easily be a true 82% or 98%. The interval shrinks with the square root of n, so quadrupling the test set only halves the uncertainty.
Common defaults and when to leave them
| Dataset size | Typical split | Reasoning |
|---|---|---|
| Hundreds of rows | Use cross-validation instead of a fixed validation set | A fixed validation set would be too small to be reliable |
| Thousands of rows | 60/20/20 or 70/15/15 | Each part is large enough to be informative |
| Millions of rows | 98/1/1 or similar | One percent is already 10,000 rows, so more test data adds little precision |
Large datasets let you shrink the held-out share. Small datasets cannot afford a large one.
Rare classes need extra care. If only 5% of rows are positive, a 200-row test set contains about 10 positives, and metrics such as recall become very unstable. Size the test set by the count of the rarest class you care about, not only by total rows. Stratification, covered in a later chapter, keeps that proportion consistent across parts.
Setting sizes in scikit-learn
test_size accepts a fraction or an absolute number of rows. Use an absolute number when you want a fixed test set regardless of how the dataset grows.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=100, stratify=y, random_state=42
)
print(len(X_train), len(X_test))Here the test set is exactly 100 rows and the other 469 go to training.
When you split twice for a validation set, the second fraction applies to the remainder. A 60/20/20 split needs test_size=0.2 first, then test_size=0.25 on the remaining 80%, because 0.25 × 80% = 20% of the original data.
A practical way to decide
Exercise
You have 400 labeled rows. A colleague proposes a 60/20/20 split. Give one concern and one alternative.
Show answer
A 20% test set is only 80 rows, so the accuracy estimate has an interval of roughly ±6 to ±7 points, and the 80-row validation set is similarly noisy for comparing models. An alternative is to hold out a small test set (for example 20%) and use 5-fold cross-validation on the remaining 320 rows for model selection. This uses all the training data for both learning and validation, and you keep the test set for one final check.