ENع
←Overfitting
Lesson 08 · Classic machine learning

Overfitting and generalisation

14 min10 steps5 questionsIntermediate
About this lesson

A model is only useful if it generalises: if it predicts well on data it has never seen. Fit six students' quiz scores with a straight line, then with a degree-5 curve that hits every point exactly, and watch five held-back students expose the curve. Training error keeps falling as a model gets more flexible; test error falls, then rises. Learn to spot overfitting and underfitting, why a validation set picks the model, and what more data does.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 8 · Interactive
01 / 10

Six students, six points

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.

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
Loading figure…
or ← →
Read the lesson step by step
  1. Six students, six points

    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.

    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
  2. Fit a straight line

    Fit a degree-1 polynomial, a straight line. np.polyfit(x, y, 1) picks the line with the smallest squared error. The degree sets the model's capacity: how much its curve is allowed to bend.

    w = np.polyfit(x, y, 1)
    w -> [1.414, 2.214]
    score = 1.414 * hours + 2.214
    
    degree 1: 2 numbers to learn, no bends
  3. Measure the training error

    How wrong is it? For each training point, take prediction minus true score, square it, and average. That is the mean squared error (MSE). The line misses every point a little: training MSE 0.23.

    err = np.polyval(w, x) - y
        -> [-0.29, 0.63, -0.46, 0.46, -0.63, 0.29]
    mse = np.mean(err ** 2)
        -> 0.2286  (shown as 0.23)
  4. A degree-5 curve

    Now allow degree 5. Six coefficients can pass through six points exactly, so the curve threads every training point. Training MSE drops to 0.00. Judged on its training data, this model looks perfect.

    w = np.polyfit(x, y, 5)
    6 numbers to learn, 6 points to hit:
    one curve passes through all of them
    
    degree 1  train MSE 0.23
    degree 5  train MSE 0.00
  5. New students: the test set

    Five more students were held back from training: the test set. Between the points it memorised, the wiggly curve swings up and down and misses them. Its test MSE is 0.93, against 0.26 for the straight line.

    test data (never used for training)
      0.5  3.25  curve says 1.50
      1.5  3.75  curve says 4.62
      2.5  6.25  curve says 5.75
      3.5  6.75  curve says 6.87
      4.5  9.25  curve says 10.00
    
    test MSE  degree 5: 0.93   degree 1: 0.26
  6. That is overfitting

    This is overfitting: the curve fitted the noise in six examples, not the trend behind them. Training error low, test error high. The line generalises: about as good on new students (0.26) as on old ones (0.23).

               train MSE   test MSE
    degree 1   0.23        0.26    generalises
    degree 5   0.00        0.93    overfits
    
    a decision tree grown until every leaf holds
    one example memorises its data the same way
  7. Too simple: underfitting

    Go the other way. Degree 0 is a flat line at the average score, 5.75. It cannot follow the trend at all, so both errors are high: training 6.06, test 4.75. That is underfitting.

    w = np.polyfit(x, y, 0)
    w -> [5.75]   (the mean score)
    
    train MSE 6.06   test MSE 4.75
    too little capacity: misses the trend itself
  8. Error against complexity

    Side by side: as the degree rises, training error only falls: 6.06, 0.23, 0.00. Test error falls, then rises again: 4.75, 0.26, 0.93. Choose the degree where test error is lowest, not training error.

    degree   train MSE   test MSE
      0      6.06        4.75
      1      0.23        0.26
      2      0.23        0.26
      3      0.17        0.27
      4      0.17        0.27
      5      0.00        0.93
    training error never rises; test error is U-shaped
  9. Pick with held-out data

    Using the mint test points to choose the degree turns them into a validation set. Keep one more set locked away and measure it once, at the end, for an honest score. Cross-validation takes turns holding out each part.

    training set     fit the coefficients
    validation set   choose the degree
    test set         measure once, at the very end
    
    k-fold cross-validation: split into k parts,
    hold out each part in turn, average the errors
  10. More data calms it

    More data helps. Fit the same degree-5 curve to all 11 points: it can no longer pass through every one, and it stays within 0.6 of the green straight line (before: 1.4). Other cures: a simpler model, regularisation, early stopping.

    w = np.polyfit(x_all, y_all, 5)   # 11 points
    train MSE 0.17   (no held-out data left)
    furthest from the line: 0.57  (was 1.42)
    
    regularisation: penalise large coefficients
    early stopping: stop training when the
      validation error starts to rise

Quiz Unlocks after the last step.