A plain linear model can overfit — chasing noise with large coefficients. Regularization adds a penalty on coefficient size, shrinking them toward zero for a simpler, more generalisable model.
Ridge vs Lasso
| Model | Penalty | Effect |
|---|---|---|
Ridge | L2 (sum of squares) | Shrinks all coefficients smoothly |
Lasso | L1 (sum of absolutes) | Drives some coefficients to exactly 0 (feature selection) |
ElasticNet | L1 + L2 | A blend of both |
from sklearn.linear_model import Ridge, LassoRidge(alpha=1.0).fit(X_train, y_train) # larger alpha → more shrinkageLasso(alpha=0.1).fit(X_train, y_train) # some coefficients become exactly 0
Key takeaways
- 01Regularization penalises large coefficients to reduce overfitting.
- 02Ridge (L2) shrinks all coefficients; Lasso (L1) zeroes some out (feature selection).
- 03alpha controls strength — tune it with cross-validation.
- 04Always scale features before regularised regression.
