Classical Machine Learning, lesson 4
Regularisation with L1 and L2
Charge the model for big weights, and see which ones shrink or vanish.
"Linear regression" (coefficients and mean squared error) and "Training, validation and test sets" (overfitting, and choosing on validation rows).
Part 1. Wild weights
Go back to the degree-11 curve from "Training, validation and test sets", the one that passed through all 12 points. Solve for its weights exactly, with x running from 0 to 1, and you find +24 million, −29 million and +22 million side by side. The degree-3 curve's largest weight is about 27.
Huge weights of opposite sign cancel almost perfectly at the 12 training points. Between them, a small change in x upsets the balance and the curve swings. That's what overfitting looks like from the inside.
Near-copy columns do the same thing in an ordinary table. Say two listing sites were merged: one gives each flat's area in m², the other in square feet, rounded to the nearest 10. The two columns carry the same information, apart from the rounding.
Your turn
A plain least-squares fit on 20 such flats gives −212 dollars per m² and +21.6 dollars per square foot. What does it predict for a flat 1 m² (10.76 sq ft) bigger than another?
Both problems have the same cure: make big weights cost something. Instead of fitting to make the error as small as possible, fit to make the error plus a charge on the size of the weights as small as possible.
Example 1.1
Two scores added together
- training error+α × size of the weights
The fit now balances two things: matching the training rows, and keeping the weights small. α (alpha) sets how much the second one counts.
- α = 0plain least squares
With α = 0 the charge vanishes, and you get the plain fit, wild weights and all.
- α hugeevery weight near 0→predict the average
With a huge α, any weight costs more than it saves, so the model flattens out and predicts the average every time: underfitting.
- α in betweenweights only as big as the data justifies
In between, a weight has to earn its size by cutting a lot of error. You find that α by trying several and scoring each on validation rows.
Regularisation
Adding a charge for large weights to the score a model is fitted on, so it prefers simpler answers. Its strength, α, is a setting you choose, not a number the fit learns.
Part 2. L2: ridge
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- L2: ridge
- L1: lasso
- Using a penalty well
Classical Machine Learning overviewSpotted a mistake? Tell us.