Sub topic 12
From nested estimate to a final model
This chapter assumes you can run GridSearchCV and cross_val_score. It also assumes you know what a Pipeline is and have read the previous chapters on nested cross-validation and on reading outer scores.
What the nested score describes
Nested cross-validation does not produce a model you deploy. It produces a number. In each outer fold, the inner search picks hyperparameters on the outer training set, and the outer test fold scores the result. Each outer fold may end with a different model and different hyperparameters.
So the nested score estimates the performance of the whole procedure: "scale the data, then run this grid search with this inner CV, then refit on the training data." It is not the score of any single fitted model.
Refit the procedure on all the data
Once the nested estimate looks acceptable, run the same tuning procedure one time on every sample. The outer folds have done their job, so there is no reason to hold data back any longer. More training data usually gives a model that is at least as good as the fold models.
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]}
# Final model: rerun the whole selection procedure on all the data.
final_search = GridSearchCV(pipe, param_grid, cv=cv)
final_search.fit(X, y)
final_model = final_search.best_estimator_GridSearchCV has refit=True by default. After choosing the best parameters by cross-validation, it refits the pipeline on all the data it was given. That refitted pipeline is best_estimator_. The scaler is refitted too, because it sits inside the pipeline.
What to report
Report two things, and keep them apart:
- The nested score, such as the mean and standard deviation of the outer scores. This is your estimate of how the procedure performs on new data.
- The final model, fitted on all data. You use it for predictions.
Do not report final_search.best_score_ as the expected performance. It is the optimistic number from the earlier chapters. The nested score is the honest one.
Common mistakes
| Mistake | Why it is a problem |
|---|---|
| Deploying one of the outer-fold models | It was trained on only part of the data, and it is just one draw from a procedure that varies by fold. |
| Picking the most common per-fold hyperparameters and skipping the search | The final fit then differs from the procedure you evaluated, so the nested score no longer describes it. |
| Using `best_score_` as the performance estimate | It is the maximum over candidates on the folds that chose it, so it is biased upward. |
| Changing the grid or the pipeline after seeing the nested score | You are now tuning on the outer folds, and the estimate becomes optimistic again. |
If the per-fold hyperparameters differ a lot, the final search may pick yet another setting. That is not an error. It tells you the score is fairly flat across the grid, or that the data is too small to separate the candidates.
Exercise
A colleague runs nested cross-validation and gets 0.97 accuracy. They then deploy the model from the third outer fold, because it scored best. What is wrong with this, and what should they do instead?
Show answer
The nested score estimates the whole tuning procedure, not any one fold model. The third-fold model saw only part of the data, and choosing it because it scored best on its own test fold adds selection bias. They should rerun the same GridSearchCV on all the data and deploy its bestestimator. They should report 0.97 as the estimate for that procedure.
Exercise
Why is it still fine to call fit on all the data in the final step, given that earlier chapters warned against evaluating on data used for selection?
Show answer
The final fit is for producing a model, not for estimating performance. The performance estimate already came from the outer folds, where no test fold influenced selection. You do not score the final model on the same data and report that score.