Sub topic 16
Interpreting and reporting results
This chapter assumes you can run GridSearchCV or RandomizedSearchCV, know what a pipeline is, and have read the chapters on cross-validation and nested cross-validation. A finished search returns a best setting, but that setting is only an estimate with noise around it. This chapter covers how to read the scores and what to report.
Read the whole table, not just the winner
best_params_ hides how close the other settings were. Every scikit-learn search stores per-setting results in cv_results_. Load it into a DataFrame and look at the top rows.
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV, 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)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0, stratify=y
)
pipe = make_pipeline(StandardScaler(), SVC())
param_grid = {
"svc__C": [0.1, 1, 10],
"svc__gamma": [0.001, 0.01, 0.1],
}
search = GridSearchCV(pipe, param_grid, cv=5)
search.fit(X_train, y_train)
results = pd.DataFrame(search.cv_results_)
cols = [
"param_svc__C",
"param_svc__gamma",
"mean_test_score",
"std_test_score",
"rank_test_score",
]
top = results[cols].sort_values("rank_test_score").head(5)
print(top.to_string(index=False))Compare mean_test_score with std_test_score. If the top five means differ by less than one standard deviation, the ranking is mostly noise. Any of those settings is a defensible choice.
Check where the best value sits
Look at where the best value falls within each range.
- If it sits on the edge of a range, the true optimum may lie outside it. Extend the range and rerun.
- If scores are flat across a hyperparameter, it barely matters. Fix it and stop spending budget on it.
- If scores change sharply between neighboring values, the model is sensitive. Use a finer scale there.
Prefer simpler settings when scores tie
When several settings tie within noise, choose the cheapest or most regularized one. This is the one-standard-error rule: take the simplest setting whose mean score is within one standard error of the best. A simpler model is usually more stable on new data and cheaper to run.
Report three different numbers
Keep these numbers separate, because they answer different questions.
| Number | Source | What it means |
|---|---|---|
| Best CV score | `best_score_` | Optimistic. You picked the maximum of many noisy scores. |
| Nested CV score | Outer folds of nested CV | Honest estimate of tuning plus model. |
| Test score | One final evaluation on held-out data | Estimate for the chosen model, used once. |
The best CV score is biased upward because selecting the maximum of noisy values favors lucky folds. Do not present it as expected performance. Score the test set once, after all choices are final. If you tune again after seeing the test score, it becomes a validation set.
What a reproducible report contains
- The metric you optimized and why.
- The search space, including scales (for example
Clog-uniform from 0.01 to 100). - The search method, the number of trials, and the cross-validation scheme.
- Random seeds for the data split, the search, and the model.
- Library versions and total compute time.
- The top few settings with mean and standard deviation, not only the winner.
- Test or nested CV scores, labeled as such.
Exercise
A search reports bestscore of 0.97 and a single test accuracy of 0.94. A colleague says tuning failed because the scores disagree. What is a better reading, and what would you do next?
Show answer
The gap is expected. bestscore is the maximum over many noisy cross-validation scores, so it is biased upward, while the test score is an unbiased estimate from one evaluation. Check stdtestscore and the test set size to see whether 0.03 is within noise. For an honest estimate of tuning plus model, run nested cross-validation. Report the test score as the performance estimate and do not retune on it.