Skip to content
You Start with one site
Amounts in Start with one site
Machine Learning · Concept

Overfitting

One idea Beginner 5 min read
Found this useful?

A student learns last year's examination paper by heart, with every answer. On that paper the student scores full marks, and on this year's paper, with new questions, the student fails. A model can do the same: overfitting is when a model fitted to data follows its own examples so closely that it gives poor answers on anything new.

Too simpleA straight line through points that bend. Wrong on old data and on new data.Both errors high
About rightFollows the shape and ignores the scatter.Both errors low
Too flexiblePasses through every old point, scatter included.Perfect on old, poor on new

See it

Predict first

A curve is fitted to 8 days of data. You give the curve more freedom until it passes exactly through all 8 days, so its error on them is zero. What happens to its error on 8 other days that it has never seen?

See it: one curve, fitted to 8 days and tested on 8 others

Error on the training days
Error on the new days
Numbers the curve can adjust

Each point is one day: the outdoor temperature and the average cooling power of a building. The curve is fitted to the round points only. The square points are days it has never seen. The error is the RMS error: square each miss, take the average, then the root. Complexity is the degree of the curve: 1 is a straight line, and each step adds one more bend.

The idea

Measured data are a real pattern plus scatter: sensor noise, a door left open. A model with few adjustable numbers can only follow the pattern. With many, it follows the scatter too, and the scatter of next week will be different.

The only way to see this is to keep some data aside. The training data are used to fit the model. The test data are never shown to the model, and are used once, to measure the error that the model will make in service.

As a model gets more complex, its training error always falls. Its test error falls at first, then turns and rises. The best model is at the turn. The lesson on practical model fitting shows how to find it.

One limit: a test is only as wide as its data. A model tested on hot days has not been tested for cold ones.

One worked number

The question

Two models predict the cooling power of a building, which is about 60 kW on an average day. Model A has an error of 2.40 kW on its training days and 2.72 kW on new days. Model B has 0.00 kW on its training days and 10.13 kW on new days. Which one should predict tomorrow?

Show the worked solution, 3 steps
  1. Ignore the training error as a score

    Model B looks perfect at 0.00 kW. That number only says that B can repeat what it was given.

  2. Compare the errors on new days

    A: 2.72 kW. B: 10.13 kW. 10.13 ÷ 2.72 = 3.7, so B is almost 4 times worse on days it has not seen.

  3. Read the gap

    A: 2.72 − 2.40 = 0.32 kW. B: 10.13 − 0.00 = 10.13 kW. A large gap between the two errors is the sign of overfitting. Use model A. In the figure, A is complexity 2 and B is complexity 7.

The mistake to avoid

A low error on the training data does not show that a model is good. It shows that the model can reproduce what it was given. Ask for the error on data the model never saw. If nobody kept such data aside, the quality of the model is not known.

A second trap: if you try twenty models and keep the one with the best test error, that error is no longer a fair test. Keep a last set of data for the final check.

Check yourself

Question 1

A model of the power of a pump has an error of 0.5 kW on its training data and 4.0 kW on test data. What error should you expect when the model is in service?

Question 2

A model of a 60 kW cooling plant has an error of 6.1 kW on its training data and 6.3 kW on test data. What is the problem?

Question 3

A model overfits: 0.3 kW on training data, 5 kW on test data. Which change is most likely to help?

Where you meet it

  • A fault detector on a pumpA model that learned one dry season raises false alarms when the rains change the load.
  • An energy model of a buildingFitted to a few months, it follows those months and predicts the next season badly.
  • A camera that checks parts on a lineTrained under the lighting of one shift, it rejects good parts when the lamps or the batch change.
Key points
  • Overfitting: the model follows the scatter of its own examples and fails on new data.
  • The training error always falls as a model gets more complex. The test error falls, then rises.
  • Judge a model only on data that were kept aside and never used to fit it.
  • A large gap between training error and test error is the sign.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.