Overfitting and underfitting
Last updated
A model can be too simple to catch the pattern in the data, or so complex it catches the noise too. Both do badly on new data, for opposite reasons. This lesson turns one dial, k in k-NN, from one extreme to the other, watches training and test error move, and names what’s going on: underfitting, overfitting, and the bias and variance behind them.
By the end of this lesson you can
- Spot overfitting and underfitting from training and test scores
- Read a plot of training and test error against model complexity, and find the sweet spot
- Explain bias and variance in words, and show them by retraining on fresh data
k = 1 against k = 151
The penguins are almost too easy: most settings do well on them. To see the problem clearly, this lesson uses two classes of points shaped like interlocking half-moons, with lots of noise, so they overlap. The figure trains -NN on 200 of them and tests it on 100 more. Turn the dial from simple to complex:
Working out the errors…
- At , every training point rules the space around it. The borders wrap round single noisy points, making islands. Training error is 0; test error is worse than it needs to be. The model has learned the noise. That's overfitting.
- At , half the training data votes on every point. The border is nearly straight and misses the moons' curve. It's poor on training and test data alike. The model is too simple for the pattern. That's underfitting.
- Somewhere in between, it follows the curve without chasing every point.
Here are the same kinds of numbers from scikit-learn, with the data made by make_moons (the figure's moons are drawn the same way, but they aren't the identical points, so the numbers differ a little):
Error against model complexity
Plotting both errors for every shows the shape you'll see again and again in this course:
Read it from left (simple) to right (complex):
- Training error falls steadily as the model gets more complex, all the way to 0 at . A complex enough model can fit its training data perfectly.
- Test error falls at first, as the model gets flexible enough to follow the real pattern, then rises again as it starts fitting noise. It's roughly a U (noisy, because 90 test points is not many).
- The best model is near the bottom of the U, not at the end of either curve. Here that's around 51 to 101, with a test error of 10%.
The gap between the two curves is a warning light: small on the left, big on the right. Every kind of model has a dial like : the depth of a tree (Stage 4), the size of a network (Stage 6), how strongly the weights are held back (regularisation, lesson 3.8). Finding the bottom of the U honestly is lesson 2.9.
Bias and variance
Two words explain the U. Imagine you could collect a fresh training set many times and train the same kind of model on each:
- Variance is how much the model's predictions change from one training set to the next. A model changes a lot, because every noisy point moves its borders.
- Bias is how far its predictions are from the truth on average, however many training sets you average over. A model is nearly the same every time, but its straight-ish border is wrong in the same way every time.
You can't collect fresh penguins, but you can make fresh moons. This trains each model on 50 different training sets and asks each version the same question: how likely is class 1 at the point ?
With , the answer is 0 or 1, and it flips between training sets: a spread (standard deviation) of 0.48, the most a 0-or-1 answer can vary. With , the answer barely moves (spread 0.03), and it sits at 0.50 every time. It's stable but has stopped saying anything about this point: that's bias. is in between on both.
For squared error there's an exact version, which Stage 3 uses:
In words: the expected squared error of a model's prediction at , averaged over training sets, is its bias squared, plus its variance, plus noise in the data that no model can remove. Making a model more complex usually lowers the bias and raises the variance. The bottom of the U is where their sum is smallest.
Wrong way: picking the best training score
If you pick by the training score, you'll always pick the most complex model:
has a perfect training score, so it "wins". On the test set it gets 82.2%, against 87.8% for . The training score can only tell you whether a model is able to fit the data. It can't tell you whether it fits the pattern or the noise, because the noise is in the training data too.
So choose settings by a score on data the model didn't train on. But not the test set either: if you try many settings on the test set and keep the best, the test set has helped choose the model, and its score is no longer an honest estimate. Lesson 2.9 shows how to choose without touching it.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 5: Machine Learning Basics: capacity, overfitting and underfitting (5.2), and bias and variance (5.4)
- Geman, Bienenstock and Doursat, Neural Networks and the Bias/Variance Dilemma, Neural Computation 4(1), 1992: the paper that named the bias–variance trade-off for learning
- scikit-learn: Underfitting vs. Overfitting: the same idea, shown with curves of different degrees
- scikit-learn: make_moons: the two interlocking half-moons used in this lesson