Boss lesson · Unit 01
The polynomial that fit too well
Ten points, three polynomials and the price of a perfect fit
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.
Correct answer
Perfectly: it can pass through every one
Why
Ten coefficients are enough to thread a curve through any ten points with different x values, noise included. The training error is 0. Whether that curve is any good between the points is a separate question.
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.
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.954Concept
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.
w = np.array([3.0, -4.0])
error, lam = 2.0, 0.1
print(error + lam * (w ** 2).sum()) # 4.510 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.