g↗genai/ learner
Data and Evaluation / Lesson 07

Overfitting and underfitting

A curve can match past examples closely and still miss new ones. Compare how a model learns a pattern, misses it, or memorises random bumps.

Concept + interactive labBeginner friendly

The pattern is more useful than every bump.

A pattern is a relationship that tends to repeat. Noise is variation that does not repeat reliably. A model learns from examples, so it can pick up the pattern, noise, or some of each.

Generalisation means making useful predictions on examples the model has not learned from. Lesson 5.5 introduced training and validation sets: training examples teach the model; held-back validation examples help us check how well its predictions carry over.

A prediction error is how far a prediction misses. Training error is the average miss on training examples; validation error is the average miss on held-back validation examples. Model flexibility describes how many different shapes a model can represent.

Running example: Sam’s ice-cream cart (invented)

Sam wants to predict daily scoops sold from afternoon temperature. Sales tend to rise on warmer days, then level off on very hot days. A passing shower or local event may shift one day’s sales; those one-day bumps are noise.

Imagine Sam keeps eight past days for training and six different days for validation. The useful pattern is the broad rise and plateau, not the exact sales count on every individual day.

Underfitting misses the pattern.

Underfitting happens when a model is too limited to capture the main relationship in its examples. It tends to make large errors on both training and validation examples.

Worked example: one flat prediction

If Sam’s model predicts 63 scoops on every day, it ignores temperature. It is too simple for the rise and plateau, so it misses cool and warm days in both sets. A more flexible curve could represent that shape.

Outside this example, a one-size-fits-all jacket is like an underfitting model: its simple shape may not fit many people well.

Overfitting memorises bumps.

Overfitting happens when a model follows details in its training examples that do not carry over to new examples. It can have very low training error and much higher validation error.

Worked example: Sam’s wiggly curve

A wiggly curve can pass through all eight training days, including a shower-day bump. If the wiggle swings away from the six validation days, the close training match was misleading. It memorised the training details instead of capturing only the pattern.

Think of an exam: memorising answers on one practice sheet may not help with new questions.

A good fit captures the trend.

A good fit captures the repeatable pattern without trying to match every noisy point. Its predictions tend to stay useful on held-back examples too. “Good” here means a useful balance for this data, not a guarantee about every future case.

Worked example: Sam’s smoother curve

A smoother curve follows the rise and plateau in Sam’s sales. It may miss a few noisy training points, but can predict the six held-back days better than a curve that chases each bump.

Learning the ideas behind exam questions is more likely to carry over than memorising one answer sheet, though no practice can cover every possible question.

Fit the pattern, not every bump: the plots compare Training days with Unseen days. Underfit misses trend, Good fit captures trend, and Overfit chases noise. Analogy: memorising one sheet may miss new questions. Key takeaways explain how underfitting, overfitting, and a useful fit relate to the pattern.
The three curves see the same points: the smooth trend carries farther than a flat line or a curve that chases each bump.

Compare both errors to diagnose the fit.

Recall that training error measures average misses on training examples, while validation error measures misses on held-back examples. “Low” depends on the task, so compare the two errors as well as their size.

What you seeWhat it may suggest
High training and validation errorsUnderfitting: the main pattern may be missed.
Low, similar errorsA useful fit that may generalise to new examples.
Very low training, much higher validation errorOverfitting: training details may not carry over.

As model flexibility increases, training error often falls. Validation error may fall at first, then rise if the model starts following training noise. These curves are a useful guide, not a rule that every real model follows exactly.

Compare both errors: Training error alone can mislead. The chart plots Training error and Validation error as Model flexibility moves from Simple to Complex, with Underfit, Good fit, Overfit, and a Wider gap. Analogy: practice results can hide race-day misses. Key takeaways say high errors can suggest underfitting, low close errors a useful fit, and a wide gap overfitting.
A growing distance between training and validation error can warn that a model is fitting details that do not generalise.

Try the curves on Sam’s days.

Use the controls in order: inspect the training view, reveal the validation days, then compare all three curve shapes and their average errors.

These six days are held out while each curve is fit. Because we compare curves using them, they guide a choice; they are not an untouched final test. In practice, reserve separate test data for a final check.

  1. Start on Too flexible (Overfit) with unseen days hidden—notice the 0.0 scoops training error.
  2. Click Show unseen validation days to reveal six new days the curve never saw.
  3. Switch between Too simple, Balanced, and Too flexible and compare both average errors.
Model flexibility
2040608010014°C18°C22°C26°C30°CAfternoon temperature (°C)Scoops sold
Training days (8)Fitted curve
Training error (avg. miss)0.0 scoops
Validation error (unseen days)Hidden
Too flexible (Overfit) — training view only

On training days alone this looks like a perfect 0.0-scoop fit—turn on unseen validation days to see if it holds up.

Illustrative simulation: these temperatures, scoop counts, and curves are invented to demonstrate fitting behaviour, not measurements from a live model.

✎ Try it yourselfKnowledge check · Practice only

Check your understanding.

In the invented ice-cream example, the wiggly curve has an average miss of 0 scoops on Sam’s eight training days and 21 scoops on the six held-back validation days. What does this most strongly suggest?

Take this with you.

A perfect training fit is not automatically best. Underfitting misses the pattern; overfitting follows training noise; a useful fit captures the pattern and carries over to unseen examples.

Compare training error with validation error. High errors on both can suggest underfitting, low and similar errors can suggest a good fit, and a wide gap can suggest overfitting.