Sub topic 2
Why settings matter: generalization, overfitting, and underfitting
This chapter assumes you can train and score a scikit-learn model and know what a train/test split is. It explains why the settings you choose change how well a model works on new data.
Generalization is the goal
A model is useful only if it works on data it has not seen. This ability is called generalization. Training accuracy tells you how well the model remembers its training rows. It does not tell you how well the model will do later.
The gap between training performance and performance on unseen data is the generalization gap. Hyperparameters, the settings you choose before training, are the main way you control that gap.
Two ways to fail
- Underfitting: The model is too simple to capture the pattern. Training and validation scores are both poor.
- Overfitting: The model is flexible enough to memorize noise in the training set. The training score is high and the validation score is clearly lower.
| Symptom | Likely cause | Typical fix |
|---|---|---|
| Low train score, low validation score | Underfitting | Allow more complexity, such as a deeper tree or weaker regularization |
| High train score, much lower validation score | Overfitting | Reduce complexity, such as a shallower tree or stronger regularization |
| High train score, similar validation score | Good fit | Keep the setting |
Many hyperparameters act as a complexity dial. Turning the dial one way causes underfitting and the other way causes overfitting. Tuning means finding the setting between the two.
Seeing it with one hyperparameter
The max_depth of a decision tree limits how many questions the tree can ask. This code uses validation_curve to compare training and cross-validated scores across depths.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import validation_curve
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
depths = [1, 2, 3, 5, 8, 12, 20]
train_scores, val_scores = validation_curve(
DecisionTreeClassifier(random_state=0),
X,
y,
param_name="max_depth",
param_range=depths,
cv=5,
)
for d, tr, va in zip(depths, train_scores.mean(axis=1), val_scores.mean(axis=1)):
print(f"max_depth={d:2d} train={tr:.3f} validation={va:.3f}")Expect the training score to climb toward 1.0 as depth grows. The validation score usually rises at first, then flattens or dips. The depth where validation is highest is the better setting, even though its training score is lower than the deepest tree's.
Common mistakes
- Choosing by training score. The training score almost always favors the most complex setting. Compare settings on validation data.
- Reading small differences as real. Validation scores come from finite data and vary. A gap of 0.002 between two settings is often noise.
- Assuming one dial fits every model. Some hyperparameters, such as the number of trees in a random forest, rarely cause overfitting on their own when raised. Check how each one behaves.
- Blaming the model when the data is the issue. If both scores are low at every setting, more tuning will not help. Better features or more data may.
Exercise
A random forest has a training accuracy of 0.99 and a cross-validated accuracy of 0.82. Is this more likely underfitting or overfitting, and which direction should you move a complexity hyperparameter such as max_depth?
Show answer
It is overfitting, because the training score is far above the validation score. Reduce complexity, for example by lowering maxdepth or raising minsamples_leaf, then recheck the gap.