Bitpath

Boss lesson · Unit 01

The polynomial that fit too well

Ten points, three polynomials and the price of a perfect fit

13 exercisesabout 3 min

What you practise

  • Fit degrees 1, 3 and 9 to the same ten points
  • Training error falls while test error explodes
  • L2 and L1 penalties shrink the weights
  • Bias against variance

Try a question from this lesson

Pick one

You have 10 noisy points from a gently curving trend. A degree-9 polynomial has 10 coefficients to tune. How well can it fit those 10 points?

Hint

Ten unknowns, and ten conditions to satisfy.

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Test error tells the truth

Here x, y are the 10 training points, xt, yt are 50 fresh points from the same curve, and mse is the mean squared error of a polynomial on a set of points.

Training error only falls as the degree rises. Test error falls, then climbs: the degree-9 curve swings wildly between the points it memorised. That is overfitting.

Python
for deg in [1, 3, 9]:
    c = np.polyfit(x, y, deg)
    print(deg, mse(c, x, y).round(3),
          mse(c, xt, yt).round(3))
# 1 0.231 0.305
# 3 0.095 0.098
# 9 0.0 0.954

Concept

Pay for large weights

Regularisation adds a penalty to the loss, so the model prefers small weights:

loss = error + lambda × (sum of squared weights)

This is the L2 penalty, also called ridge. The multiplier lambda sets the price. At 0 nothing changes; as it grows, the fit is pulled towards a flatter, calmer curve.

Python
w = np.array([3.0, -4.0])
error, lam = 2.0, 0.1
print(error + lam * (w ** 2).sum())  # 4.5

10 more exercises in the app

  • 2× type the answer
  • 5× multiple choice
  • 1× match the pairs
  • 1× fill the blanks
  • 1× true or false

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy