0123450510hours studied →↑ quiz score1w = np.polyfit(x, y, deg)2err = np.polyval(w, x) - y3mse = np.mean(err ** 2)degreetrain MSEtest MSEDEGREE 0DEGREE 1DEGREE 5traintest6.064.75traintest0.230.26traintest0.000.93
Board: hours studied (left to right) against quiz score (to the back)Khaki pegs and bars: training data · mint pegs and bars: test dataCurves: blue degree 0, green degree 1, rose degree 5, violet degree 5 on 11 pointsThe chart board holds six training points: hours studied and quiz score. A straight line misses each a little; a degree-5 curve hits every one exactly. Five held-back test points show the truth: the wiggly curve misses them (test MSE 0.93) far more than the line does (0.26). That is overfitting; the flat degree-0 line, bad on both, is underfitting. The bars show the pattern: training error only falls as the degree rises, test error falls and then rises. Bar height grows with the square root of the error, so small errors stay visible; the numbers on the bars are the exact values. The scores are invented for the lesson: a straight trend (2 + 1.5 points per hour) plus or minus half a point of noise, so you can check every number. Real datasets are bigger and messier.

Fitting the data

  1. A tutor records six students: hours studied (0 to 5) and quiz score out of 10. These points are the training data. The real goal is to predict the score of a new student the model has never seen.

Generalising

Choosing and fixing

What happens

;; step 1/10 · data
training data (hours studied, quiz score)
  0    2.5
  1    3
  2    5.5
  3    6
  4    8.5
  5    9

goal: predict scores for NEW students,
not just describe these six

The scores are invented for the lesson: a straight trend (2 + 1.5 points per hour) plus or minus half a point of noise, so you can check every number. Real datasets are bigger and messier. In this small example the five held-out students play two parts: first a test set, then (when used to choose the degree) a validation set. In real work keep them separate, so the final test score is still honest.