Sub topic 6
Why the best cross-validation score is too optimistic
This chapter assumes you know k-fold cross-validation, the difference between hyperparameters and learned parameters, and how GridSearchCV works in scikit-learn. It explains why the score a grid search reports for its winner is biased upward, and it sets up the fix in the next chapters.
The score you get from a plain grid search
A grid search fits every candidate hyperparameter setting on the same cross-validation folds. It then reports best_score_, the highest mean fold score among the candidates.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV, StratifiedKFold
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)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
pipe = make_pipeline(StandardScaler(), SVC())
param_grid = {"svc__C": [0.1, 1, 10], "svc__gamma": ["scale", 0.01, 0.001]}
search = GridSearchCV(pipe, param_grid, cv=cv)
search.fit(X, y)
# This is the score of the best candidate on the same folds used to pick it.
print("best_score_ (optimistic):", search.best_score_)
print("best_params_:", search.best_params_)This number is a fine way to rank candidates. It is a poor estimate of how the chosen model will do on new data.
Where the bias comes from
Every candidate's cross-validation score is its true performance plus noise. The noise comes from the finite sample: some folds happen to suit one setting slightly better than another.
Picking the maximum of several noisy numbers favors the candidates whose noise was positive. The winner is partly the best model and partly the luckiest one. So the expected value of the maximum is higher than the true performance of the selected model.
This is called selection bias. Two things make it larger:
- More candidates. With more draws, the largest noise value tends to be bigger.
- Smaller datasets or noisier scores. The noise is larger relative to real differences between candidates.
A grid of three settings on a clean dataset gives a small bias. A random search over hundreds of settings on a few hundred rows can give a large one.
Why a held-out fold does not rescue you
Each fold does act as a test set for the model trained on the other folds. But across the whole search, every fold has been used to compare candidates. Information from all the data has influenced which setting won. The data that produces the reported score is the same data that made the selection, so the score is no longer an independent test.
What an honest estimate needs
The reported score must come from data that played no part in choosing the hyperparameters. The thing to evaluate is not one fitted model but the whole procedure: "run a grid search, take the winner." That procedure has to be tested on data it has not seen.
The holdout split does this, but it wastes data and gives a noisy result. Nested cross-validation applies the same idea repeatedly. An inner loop does the selection inside each outer training set, and an outer loop scores on folds that the inner loop never touched. The next chapters build it.
Exercise
A colleague runs a random search over 500 hyperparameter settings on a dataset of 200 rows and reports best_score_ as expected test accuracy. Will the true accuracy more likely be above or below that figure, and why?
Show answer
Below. With 500 candidates and only 200 rows, the fold scores are noisy, and the maximum of many noisy scores is inflated by the candidates that got lucky. The same folds chose the winner and produced its score, so the reported value is biased upward.