Sub topic 9
Generalization, Overfitting, and Underfitting
This chapter assumes you can run Python with scikit-learn, know what features and labels are, and understand that training means fitting a model to labeled examples by minimizing a loss. Here you will learn why a model that scores well on its training data can still fail on new data, and how to detect that.
Generalization is the real goal
A model is useful only if it works on examples it has never seen. This ability is called generalization. Training loss measures how well the model fits the examples it was fitted to. It says little about how it will behave on new ones.
Two failure modes follow from this.
- Underfitting: the model is too simple to capture the pattern. It does poorly on both training and test data.
- Overfitting: the model memorizes details and noise specific to the training set. It does very well on training data and noticeably worse on test data.
| Symptom | Training score | Test score | Likely cause |
|---|---|---|---|
| Both low | Low | Low | Underfitting |
| Large gap | High | Much lower | Overfitting |
| Both high, small gap | High | High | Good generalization |
Model complexity and the trade-off
Complexity is how flexible a model is. A depth-1 decision tree can make one split. An unrestricted tree can keep splitting until every training example is isolated. As complexity grows, training error keeps falling, but test error falls and then rises again.
The best model sits near the minimum of the test curve. The numbers in the chart are illustrative, not measured.
Measuring the gap
Compare the training score with the score on a held-out test set. This example uses a very shallow tree and an unrestricted one.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y
)
for depth in (1, None):
tree = DecisionTreeClassifier(max_depth=depth, random_state=0)
tree.fit(X_train, y_train)
print(depth, tree.score(X_train, y_train), tree.score(X_test, y_test))Read the output like this:
- With
max_depth=1, the tree can separate only one class from the other two. Both scores are low, which indicates underfitting. - With
max_depth=None, the training accuracy is 1.0 because the tree can fit every training point. The test score is usually a little lower. On a small, clean dataset like iris the gap is small, but the pattern is the one you look for on real data.
Exact numbers depend on the split, so run the code yourself.
Common errors
- Judging a model by training accuracy alone. A perfect training score can mean memorization.
- Tuning against the test set. If you adjust settings repeatedly based on test results, the test score becomes optimistic. Keep a separate validation set, or use cross-validation, for tuning, and touch the test set once at the end.
- Treating a small gap as proof of success. If both scores are low, the model underfits even though the gap is small.
- Ignoring data size. Small datasets make overfitting easier, and the test score itself becomes noisy.
What to do about each problem
To reduce overfitting:
- Collect more training data.
- Simplify the model, for example by limiting
max_depth. - Add regularization, which is a penalty on large parameter values.
- Remove noisy or irrelevant features.
To reduce underfitting:
- Use a more flexible model.
- Add informative features.
- Reduce regularization.
- Train longer, if the model trains iteratively.
Exercise
A classifier gets 99% accuracy on its training set and 71% on its test set. What is the likely problem, and name two changes that could help?
Show answer
The model is overfitting, because the training score is far above the test score. Two helpful changes are limiting model complexity (for example, a smaller max_depth) and collecting more training data. Adding regularization or removing noisy features would also help.
Exercise
A model scores 58% on the training set and 57% on the test set. Is it overfitting? What would you try?
Show answer
No. Both scores are low and close together, which indicates underfitting. Try a more flexible model or add more informative features.