Sub topic 2
Underfitting and overfitting
This chapter assumes you know supervised learning basics (features, labels, train and test sets), can run scikit-learn estimators with fit and predict, and know what a hyperparameter is. It builds on the previous chapter's comparison of training and validation scores.
What the two failures look like
A model can fail in two opposite ways. Both show up in the pair of scores you already know how to compute.
- Underfitting: the model is too simple to capture the pattern in the data. It scores poorly on the training data and poorly on validation data. Both scores are low, and they are close together.
- Overfitting: the model is flexible enough to memorize noise and quirks of the training set. It scores very well on training data but noticeably worse on validation data. The training score is high and the gap to the validation score is large.
A model that fits well sits between these. Its validation score is as high as the data and model family allow, and the gap is small.
| Pattern | Training score | Validation score | Gap |
|---|---|---|---|
| Underfitting | Low | Low | Small |
| Good fit | High | High | Small |
| Overfitting | High | Clearly lower | Large |
"Low" and "high" are relative to what you need. A training accuracy of 0.70 is low if a simple baseline reaches 0.90, and acceptable if the task is noisy and 0.72 is the best anyone gets.
Seeing it with a decision tree
The depth of a decision tree controls how flexible it is. A tree with max_depth=1 can only ask one question. A tree with no depth limit can keep splitting until every training sample is classified correctly.
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_validate
from sklearn.tree import DecisionTreeClassifier
X, y = load_digits(return_X_y=True)
for depth in [1, 5, None]:
model = DecisionTreeClassifier(max_depth=depth, random_state=0)
result = cross_validate(model, X, y, cv=5, return_train_score=True)
train = result["train_score"].mean()
validation = result["test_score"].mean()
print(f"max_depth={depth}: train={train:.3f} validation={validation:.3f}")Run it and compare the three lines. With max_depth=1, both scores are low, which is underfitting. With max_depth=None, the training score is 1.000 because the tree memorizes the training folds, while the validation score is clearly lower, which is overfitting. The middle setting usually gives the best validation score. The exact numbers depend on your scikit-learn version, but the pattern holds.
The argument return_train_score=True matters. By default, cross_validate reports only validation scores, and without the training score you cannot tell underfitting from a good fit.
A decision path
The flowchart summarizes how to classify a model from its two scores.
Common mistakes
- Judging by the training score alone. A high training score says nothing about new data. Overfit models often have the best training scores.
- Calling every gap overfitting. Some gap is normal, because validation data is always unseen. Worry when the gap is large and the validation score is below what you need.
- Treating the labels as fixed properties of a model. Underfitting and overfitting describe a model on a particular dataset. The same model can underfit a small, complex problem and overfit a different one.
The next chapters explain why these patterns occur in terms of bias and variance, and then show how learning curves and validation curves let you see them across training set sizes and hyperparameter values.
Exercise
A model has a training accuracy of 0.99 and a cross-validated accuracy of 0.78. A second model has a training accuracy of 0.62 and a cross-validated accuracy of 0.60. Which one underfits, which one overfits, and what extra check do you need before deciding the second one is a problem?
Show answer
The first model overfits, because the training score is high and the gap to validation is large. The second model underfits if 0.60 is below what you need or what a reasonable baseline achieves, because both scores are low and close together. Before calling it a problem, compare 0.60 with a simple baseline and with the accuracy the task realistically allows, since a noisy task can cap both scores.