Sub topic 2
The holdout split and its limits
This chapter assumes you know what features, labels, and a fitted estimator are, and that you can run scikit-learn code. The previous chapter defined generalization error: the expected error on new data drawn from the same source as the training data. You can't measure it directly, so you estimate it. The simplest estimate is the holdout split.
How a holdout split works
You split the data once. The model trains on the larger part and is scored on the smaller part, which it never saw during fitting. That score estimates generalization error, because the test rows play the role of "new data".
In scikit-learn, use train_test_split. Pass stratify=y for classification so both parts keep the class proportions. Pass random_state so the split is reproducible.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_breast_cancer(return_X_y=True)
for seed in range(5):
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=seed
)
model = make_pipeline(StandardScaler(), SVC())
model.fit(X_train, y_train)
print(seed, round(model.score(X_test, y_test), 3))The model and data are the same in every iteration. Only the split changes, yet the five accuracies differ. Each one is a valid estimate, and none is "the" accuracy.
Limits of the holdout split
- High variance. The test set here has 114 rows, so one misclassified row moves accuracy by about 0.9 percentage points. Which rows land in the test set changes the score. Differences between two models that are smaller than this noise tell you nothing.
- Wasted data. The test rows never train the model. With small datasets, a 20% holdout removes data you can't spare, and the model you evaluate is weaker than one trained on everything.
- A single estimate. You get one number with no sense of its spread.
- Reuse corrupts it. A test set gives an honest score only if it is used once, after all decisions are made. Every time you look at the score and change something, the test set leaks into your choices.
The usual remedy and its cost
You can carve a third part out of the training data, called a validation set. You compare candidate settings on it, and you touch the test set only once at the end. This protects the test set, but it shrinks the training data further and makes every score depend on one more arbitrary split.
What you need is a way to use all the data for both training and scoring, with many different splits, and to average the results. That is k-fold cross-validation, the topic of the next chapter.
Exercise
A colleague runs ten hyperparameter settings, scores each on the same 20% test set, and reports the best test accuracy as the expected accuracy on new data. Name two reasons this number is unreliable.
Show answer
First, the reported number is the maximum of ten noisy scores, and the test set was used to choose the setting, so the number is biased upward. Second, a single small test set gives a high-variance estimate, because a few rows landing on one side or the other change the score. A different random split could give a noticeably different result.