Sub topic 2
The Holdout Split and Why One Split Is Not Enough
This chapter assumes you know what training error and overfitting are, can read accuracy as a metric, and have used scikit-learn's fit and predict. The previous chapter explained why training-set performance is optimistic. Here you will see the simplest fix and where it falls short.
The holdout idea
A holdout split sets aside part of the labeled data before training. You fit the model on the larger part (the training set). You then score it once on the held-out part (the test set). The model never saw the test set, so its score estimates performance on new data.
In scikit-learn this is train_test_split. Common choices are 20% to 30% for the test set. Pass stratify=y for classification so both parts keep the class proportions.
Why one split is not enough
A single holdout score depends on which rows landed in the test set. Two sources of noise follow from that:
- Test-set noise. A small test set gives a noisy accuracy. With 114 test samples and a true accuracy near 0.95, the standard error is about sqrt(0.95 × 0.05 / 114), or roughly 0.02. A difference of two points between two models can be chance.
- Training-set noise. A different split also trains the model on different rows, so the fitted model changes.
There is also a cost. Every row in the test set is a row the model cannot learn from. On small datasets, a holdout wastes data twice: once in training and again because the test set is too small to be precise.
See the variation
The script below repeats the same holdout procedure with ten different random seeds. Only the seed changes. The model and data stay the same.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
scores = []
for seed in range(10):
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=seed
)
model.fit(X_train, y_train)
scores.append(model.score(X_test, y_test))
print(np.round(scores, 3))
print(f"min={min(scores):.3f} max={max(scores):.3f} std={np.std(scores):.3f}")Run it and look at the spread between the minimum and maximum. Each number is a valid holdout estimate, yet they disagree. If you had run only random_state=0, you would have reported that one number with no sign of how much it could move.
Common errors with holdout
- Reusing the test set. If you try several models or settings and keep the one with the best test score, the test set has influenced your choice. The final score is then optimistic. A test set is only trustworthy if you score on it once, at the end.
- Picking a "good" seed. Searching for a seed that gives a high score is the same mistake in disguise.
- Preprocessing before splitting. Fitting a scaler on all the data lets test-set information reach training. Split first, or use a pipeline. A later chapter covers this.
- Ignoring structure. A random split is wrong when rows are related (the same patient) or ordered in time. Later chapters cover those cases.
The way forward
The fix for a noisy single split is to do many splits and combine the results. If every row is used for testing exactly once, you get a score for each part of the data and a measure of how much they vary. That is k-fold cross-validation, the subject of the next chapter.
Exercise
You train a classifier, try five hyperparameter settings, and report the best accuracy from a single 20% holdout test set. Give two reasons this number is unreliable.
Show answer
First, the test set was used to choose among settings, so the best score is selected by luck as well as quality and is optimistic. Second, a single small test set gives a noisy estimate, and a different random split could give a noticeably different accuracy. A separate validation set or cross-validation should be used for selection.