Sub topic 3
Bias and variance in plain terms
This chapter assumes you know what training and validation scores are, and you can run a scikit-learn estimator with fit and score. You should also know that overfitting means a model memorizes noise in its training data. Here we connect those ideas to two terms you will see in every curve discussion: bias and variance.
Two sources of error
A model's error on new data comes from two main sources.
- Bias is error from assumptions that are too rigid. A straight line fitted to a curved pattern is wrong in the same way no matter which sample you train on. High bias means the model cannot capture the real pattern.
- Variance is error from sensitivity to the particular training sample. A very flexible model changes a lot when the training data changes slightly. High variance means the model fits the quirks of one sample, and those quirks do not repeat in new data.
A third part, noise, is randomness in the labels that no model can predict. It sets a floor under the achievable error.
How the two show up in scores
You rarely measure bias and variance directly. You see their effects in two numbers: the training score and the cross-validated score.
| Situation | Training score | Validation score | Gap |
|---|---|---|---|
| High bias (underfitting) | Low | Low | Small |
| High variance (overfitting) | High | Noticeably lower | Large |
| Good fit | High | High, close to training | Small |
The level of the validation score tells you whether the model is good enough. The gap tells you whether the problem is variance.
Seeing it in code
Two decision trees on the same data make the contrast concrete. A tree limited to one split is very rigid. A tree with no depth limit is very flexible.
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, None]:
model = DecisionTreeClassifier(max_depth=depth, random_state=0)
result = cross_validate(
model, X, y, cv=5, scoring="accuracy", return_train_score=True
)
train = result["train_score"].mean()
val = result["test_score"].mean()
print(f"max_depth={depth}: train={train:.3f} validation={val:.3f} gap={train - val:.3f}")With max_depth=1, both scores are low and close together. The tree has only one split, so it cannot separate ten digit classes. This is high bias. With max_depth=None, the training score is 1.0 because the tree grows until it fits every training sample. The validation score is clearly lower. This is high variance.
The trade-off
Making a model more flexible usually lowers bias and raises variance. Making it simpler does the opposite. You are looking for a middle setting where the validation score peaks, not for zero bias or zero variance. Validation curves, covered later, show this peak by sweeping one hyperparameter.
Limits of the vocabulary
- The table is a rule of thumb. A small gap does not guarantee a good model, because both scores can be equally poor.
- "Low" and "high" are relative to your goal and to a baseline, such as a dummy classifier or human performance.
- The gap is itself an estimate. Fold-to-fold noise can make a small gap look larger or smaller than it is, so check the spread across folds before acting.
Exercise
A model scores 0.62 on training data and 0.60 in cross-validation. A second model scores 0.99 on training data and 0.81 in cross-validation. Which one has a bias problem, which one has a variance problem, and why?
Show answer
The first model has high bias: its training score is already low, and the gap is small, so it underfits. The second model has high variance: its training score is near perfect, but the validation score is 0.18 lower, so it is fitting quirks of the training sample.