Sub topic 11
Reading outer scores and per-fold chosen hyperparameters
This chapter assumes you can run GridSearchCV and cross_val_score. It also assumes you know what the inner and outer loops of nested cross-validation do. The goal here is to read what the outer loop gives you and report it honestly.
What the outer scores are
Each outer fold gives one score. That score comes from a model tuned only on that fold's outer training set and tested on data the tuning never saw. The mean of the outer scores estimates how well the whole procedure performs. The procedure is "scale, then tune an SVC by grid search, then fit".
The nested score does not describe one particular set of hyperparameters. This is the most common misreading.
Collecting scores and chosen parameters
cross_val_score returns only the scores. To also see the chosen hyperparameters, use cross_validate with return_estimator=True. Each returned estimator is the fitted search object for one outer fold.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV, StratifiedKFold, cross_validate
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)
inner_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
outer_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=2)
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=inner_cv)
result = cross_validate(search, X, y, cv=outer_cv, return_estimator=True)
for fold, (score, est) in enumerate(zip(result["test_score"], result["estimator"])):
print(fold, round(score, 3), est.best_params_, round(est.best_score_, 3))
scores = result["test_score"]
print(f"nested accuracy: {scores.mean():.3f} +/- {scores.std():.3f}")Each line shows the outer score, the parameters chosen in that fold, and the inner best_score_ for that fold. Exact numbers depend on your scikit-learn version.
What to look for
Compare the inner best_score_ with the outer score in each fold. The inner value is usually a little higher. That gap is the optimistic bias from the previous chapters, and here you can see it directly.
Then look at the chosen parameters.
| Pattern | Likely meaning | What to do |
|---|---|---|
| Same parameters in every fold | The search is stable | Report them as typical choices |
| Parameters differ, outer scores similar | Several settings perform about equally | Treat the choice as unimportant |
| Parameters differ, outer scores vary a lot | Data is small or noisy, or the grid is too sensitive | Use more data, repeats, or a coarser grid |
| Chosen value sits at the grid edge | The best value may lie outside the grid | Extend the grid and rerun |
Reporting
A report should state:
- The full procedure: preprocessing, estimator, grid, and the inner and outer splitters with their fold counts.
- The mean and standard deviation of the outer scores, and the metric used.
- The hyperparameters chosen in each fold, or a summary of how often each was chosen.
Be careful with the standard deviation. The outer folds share training data, so they are not independent. The spread is a rough indication of variability, not a precise confidence interval. Later chapters return to this when we repeat the nested procedure.
Exercise
In one outer fold, the inner bestscore is 0.975 and the outer test score is 0.947. A teammate says the outer score proves the inner search failed. What is a better reading?
Show answer
A small drop from inner to outer is expected. The inner score is the maximum over several candidates on the folds used to choose it, so it is biased upward. The outer score is measured on data the search never saw. The gap is the optimism that nested cross-validation is designed to expose, not evidence of failure.