Sub topic 1
What we are really trying to estimate
Prerequisites
This chapter assumes you know the following:
- Supervised learning basics: features, labels, and the split between training and test data.
- What overfitting means.
- Basic Python and NumPy.
- scikit-learn estimators and their
fit,predict, andscoremethods.
The question behind every score
When you train a model, you rarely care how well it handles the rows it has already seen. You care how well it will handle rows that arrive later. That quantity is the generalization error: the expected loss of the trained model on new data drawn from the same source as the training data.
You can never compute this number exactly, because it averages over data you do not have. Every evaluation method in this tutorial is a way to estimate it from the finite sample you do have. A method is good if its estimate is close to the true value on average. This property is called being unbiased. It also helps if the estimate does not swing widely from one run to the next, which is low variance.
Why the training score fails
A model is fitted to minimize error on the training rows. Its training score is therefore measured on the very data that shaped it, and it is usually too optimistic. A flexible model can memorize the training set.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
model = DecisionTreeClassifier(random_state=0).fit(X_train, y_train)
print("train accuracy:", model.score(X_train, y_train))
print("test accuracy:", model.score(X_test, y_test))An unpruned decision tree scores 1.0 on its training data. Its accuracy on the held-out rows is lower. The gap between the two numbers is the optimism you would have reported if you had trusted the training score.
The rule: score on data that played no part in choices
The fix is to evaluate on data that had no influence on the model. "No influence" covers more than fitting parameters. It also covers every decision made along the way, such as feature selection, preprocessing, and the choice of hyperparameters. If a row helped you make any of those decisions, its score is no longer an honest sample of performance on new data.
This rule is the thread of the whole tutorial. Later chapters show that a plain holdout split follows it but wastes data. They show that cross-validation uses data better, and that a hyperparameter search quietly breaks the rule unless you add a second, outer loop.
Two different goals
People often mix up two jobs that an evaluation can do:
| Goal | Question | Example |
|---|---|---|
| Model selection | Which of these candidates is best? | Choosing `C=10` over `C=1` |
| Performance estimation | How good will the chosen model be on new data? | Reporting "about 97% accuracy" |
Selection only needs the ranking of candidates to be right. Estimation needs the absolute number to be right. The same scores can serve the first goal and still mislead for the second. Nested cross-validation exists to separate these two jobs.
What comes next
The next chapters build the tools in order. First comes the holdout split, then k-fold cross-validation, then hyperparameter search. You will then see exactly where the optimism enters and how nested cross-validation removes it.
Exercise
You try five hyperparameter settings, score each on the same validation data, and report the best score as your model's expected accuracy on new data. Which of the two goals (model selection or performance estimation) does that score serve well, and which does it serve poorly?
Show answer
It serves model selection well, because it ranks the five settings. It serves performance estimation poorly. The validation data helped you choose the winner, so it is not untouched, and taking the maximum over several noisy scores biases the reported value upward.