Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 9: Overfitting, Underfitting and the Bias–Variance Trade-off

Every model in machine learning sits somewhere on a line between two failures. At one end is underfitting: the model is too simple to capture the real pattern, so it does badly on the training data and badly on new data alike. At the other end is overfitting: the model is so flexible that it memorises the training data, including its noise, and does brilliantly on examples it has seen but badly on new ones. The skill of machine learning is finding the sweet spot between them, and this lesson explains why that sweet spot exists at all.

Picture a worked example. You have data on how a plant grows as you give it more water: growth rises, then levels off, then falls as the plant is overwatered. A straight line underfits — it cannot bend, so it misses the rise and the fall. A wildly wiggly curve that passes exactly through every point overfits — it has memorised every measurement error in the data. A smooth curve that follows the rise and the fall without chasing every point is just right. The difference is the model's flexibility: too little and it cannot see the pattern; too much and it sees patterns that are not there.

Bias and variance: two kinds of error

Behind those two failures are two kinds of error, called bias and variance. Bias is error from wrong assumptions: a straight-line model assumes the world is straight, and when it is not, the model is biased — it misses the pattern even on its own training data. Variance is error from sensitivity to the particular training examples: a very flexible model changes a lot if you swap in a different set of examples, because it has latched onto the quirks of whichever set it saw. Underfitting means high bias and low variance; overfitting means low bias and high variance. The two pull in opposite directions, which is why the trade-off is unavoidable: making a model more flexible reduces bias but increases variance, and making it simpler does the reverse.

How do you know which problem you have? The training and test scores tell you. If both scores are poor, the model is underfitting — it has not even learned the training data. If the training score is excellent but the test score is much worse, the model is overfitting — it learned the training data too well. That gap between training and test performance is the clearest signal in machine learning, and it is the first thing to check whenever a model disappoints you.

A worked example of the gap

The gap between training and test scores is easier to trust when you have seen it in numbers. Imagine a worked example: a team trains a model to predict exam scores from study data. On the training data it scores 92% — an excellent result. On the test data it scores 61%. That gap of 31 points is the signature of overfitting: the model learned the training students, not the subject. The team tries a simpler model and gets 74% on training and 72% on test — worse on training, far better on test, and much more honest. Then they collect twice as much training data and retrain the flexible model: now it scores 78% on training and 76% on test. More data was the real fix, because with more examples the noise mattered less and the true pattern stood out. The moral is one you will use throughout this course: never judge a model by its training score, always look at the gap, and remember that more representative data is often a better cure than a cleverer algorithm.

The fixes follow from the diagnosis. For underfitting, give the model more room: add useful features, use a more flexible model, or train for longer. For overfitting, do the opposite: simplify the model, gather more training data so the noise matters less, or use a technique called regularisation, which penalises the model for becoming too complicated. Regularisation adds a cost for large settings, gently pushing the model towards simpler explanations. You will use all of these ideas again in the next three lessons, because judging and fixing this trade-off is most of what practical machine learning actually is.

Too simple, just right, too flexible

Underfit Good fit Overfit high bias balanced high variance

The straight line misses the curve (underfitting). The smooth curve follows the pattern. The wiggly line hits every point but has learned the noise.

Key Terms

Underfitting
When a model is too simple to capture the real pattern, so it does badly on training data and on new data.
Overfitting
When a model is so flexible it memorises the training data, including noise, and does badly on new data.
Bias
Error from wrong assumptions, such as assuming a straight line when the real pattern curves.
Variance
Error from sensitivity to the particular training examples; a high-variance model changes a lot with different data.
Bias–variance trade-off
The unavoidable balance: making a model more flexible reduces bias but increases variance, and vice versa.
Regularisation
A technique that penalises a model for becoming too complicated, pushing it towards simpler explanations.
Flexibility
How complex a pattern a model is able to fit; more flexibility can capture more, but also memorise more.

Discussion Questions

  1. A model scores 99% on training data and 60% on test data. Which problem does it have, and what would you try first?
  2. Why can't you fix overfitting simply by making the model as flexible as possible and hoping for the best?
  3. Think of studying for an exam: what would underfitting and overfitting look like in a student's revision?

Practice Questions

  1. What is the difference between underfitting and overfitting?
  2. What do bias and variance mean?
  3. Training score 95%, test score 62%. What is the diagnosis?
  4. Both training and test scores are poor. What is the diagnosis?
  5. What is regularisation, in one sentence?
Show answers
  1. Underfitting is too simple to capture the pattern; overfitting memorises the training data including its noise. An underfit model fails on training data too, while an overfit model does well on training data but badly on new data.
  2. Bias is error from wrong assumptions; variance is error from sensitivity to the particular training examples. A straight line on curved data has high bias; a wiggly curve that changes with each dataset has high variance.
  3. Overfitting. The large gap means the model learned the training data well but does not generalise to new examples.
  4. Underfitting. The model has not even learned the training data, so it needs more flexibility, better features or longer training.
  5. It penalises a model for becoming too complicated, pushing it towards simpler explanations. By adding a cost for large settings, it keeps the model from chasing noise in the data.
Keep reading to complete this lesson