We have learned how to minimize a training objective.
The ML goal is still performance on new examples.
Regularization limits how freely a model can fit the training data, trading some flexibility for potentially better generalization.
Recall KNN: a tiny neighborhood can follow sample-specific fluctuations.
| Mechanism | Weaker restriction | Stronger restriction |
|---|---|---|
| KNN neighborhood size \(k\) | Small \(k\): follow nearby examples closely | Large \(k\): average over more examples |
| Coefficient penalty strength \(\lambda\) | Small \(\lambda\): allow larger coefficients | Large \(\lambda\): discourage large coefficients |
For a linear model, one common choice is L2 regularization:
\[J_\lambda(\boldsymbol{w},b)=J_{\mathrm{train}}(\boldsymbol{w},b)+\frac\lambda2\|\boldsymbol{w}\|^2.\]
Different mechanisms, same purpose: control sensitivity to the training sample.
Too much restriction can hide useful signal. \(k\) and \(\lambda\) have no numerical equivalence.
Ames KNN using living area only — validation RMSE:
| \(k=1\) | \(k=100\) | \(k=1758\) (all training sales) |
|---|---|---|
| $75,300 | $57,700 | $80,200 |
Ames linear models using 226 encoded features — test RMSE:
| No penalty: OLS | L2 penalty: validation-selected ridge |
|---|---|
| $26,300 | $25,600 |
Compare within each row: these use different representations and evaluation splits.
The restriction can help; the strongest restriction need not be best.
Search positive \(\lambda\) values on a log scale: multiply, rather than add.
| Search | Example \(\lambda\) values |
|---|---|
| Broad search: factors of 10 | \(10^{-4},10^{-3},10^{-2},10^{-1},1,10,100\) |
| Refine near a promising \(\lambda=1\) | \(0.1,0.3,1,3,10\) |
Common mistake: \(0,1,2,3,\ldots\) misses the entire range \(0<\lambda<1\).
Include \(\lambda=0\) separately as the no-penalty baseline; zero has no logarithm.