Sub topic 1
Why we need to estimate generalization error
Prerequisites
This chapter assumes you know:
- Supervised learning basics: features, labels, and fitting a model.
- Overfitting and underfitting at a high level.
- Accuracy as an evaluation metric.
- Basic Python and NumPy.
- scikit-learn's
fitandpredictAPI.
The question behind every model
You train a model to predict data it has not seen yet. A spam filter must handle tomorrow's email. A diagnostic model must handle next month's patients. The error a model makes on that new data is its generalization error.
You cannot measure generalization error directly, because the future data does not exist yet. Cross-validation, which this tutorial builds toward, is a way to estimate it from the data you already have.
Training error is optimistic
The obvious estimate is the model's error on the data it was trained on. That number is biased low. The model was fitted to those exact samples, so it can memorize their quirks and noise as well as the real pattern.
Flexible models make this worse. A decision tree with no depth limit can fit every training sample perfectly, even when the labels are partly random.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = make_classification(
n_samples=300, n_features=20, n_informative=5, flip_y=0.2, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0
)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)
print("train accuracy:", tree.score(X_train, y_train))
print("test accuracy: ", tree.score(X_test, y_test))Here flip_y=0.2 randomly corrupts about 20% of the labels. The tree still reaches a training accuracy of 1.0, because it learns the corrupted labels too. Its accuracy on the held-out samples is much lower. The gap between the two numbers is the optimism of the training score.
Where the gap comes from
As model complexity grows, training error keeps falling. Error on unseen data falls at first, then rises once the model starts fitting noise. That rise is overfitting.
Two consequences follow:
- A low training error says little about quality. It may only mean the model is flexible.
- You cannot rank models by training error. The most complex model will usually win, and it will often generalize worst.
What an honest estimate needs
An estimate of generalization error is trustworthy only if the score comes from samples the model did not learn from. This rule has to hold for the whole workflow, not only for fit. Scaling, feature selection, and hyperparameter choices are all learned from data. If any of them see the evaluation samples, the estimate becomes optimistic again. A later chapter on data leakage covers this in detail.
A good estimate has two further properties:
- Low bias. On average, it should not systematically over- or under-state the true error.
- Low variance. It should not change much if you resample the data. A score that jumps between runs cannot support a decision.
The next chapter shows that the simplest approach, one train/test split, can fail the second test.
Common mistakes
| Mistake | Why it misleads |
|---|---|
| Reporting `model.score(X_train, y_train)` | The model has already fitted these samples |
| Choosing a model by training error | Complex models always look best |
| Preprocessing all data before evaluating | Information from the evaluation samples leaks into training |
| Treating one score as exact | Any single estimate carries sampling noise |
Exercise
A random forest scores 100% accuracy on its own training set and 82% on held-out data. Which number is closer to the generalization error, and why is the other one unreliable?
Show answer
The 82% held-out score is closer. The model was fitted to the training samples, so it can reproduce them, noise included. That makes the 100% an optimistic measure of how it will perform on new data. The held-out samples played no part in fitting, so their score is a fairer estimate, though a single split still carries sampling noise.