Why Regularize?
In linear regression, the model learns a weight w that minimises the mean-squared error (MSE) on the training data. When data is noisy or the model has many parameters, the learned weight can over-fit: it captures random fluctuations instead of the true underlying relationship. Regularization adds a penalty term to the loss function, discouraging extreme weights and effectively reducing model variance.
Two classic forms are used:
- L2 (Ridge): penalty = w². It shrinks weights toward zero smoothly, yielding a convex loss with a closed-form solution.
- L1 (Lasso): penalty = |w|. It encourages sparsity, driving some weights exactly to zero, useful for feature selection.
The strength of the penalty is controlled by λ (lambda). A small λ barely affects the loss, while a large λ dominates the objective, pulling the weight toward zero regardless of the data.
Bias-Variance Trade-off
Regularization decreases variance (model sensitivity to training data) at the cost of increasing bias (error due to simplifying assumptions). The optimal λ balances these two sources of error to minimise overall generalisation error.
Practical Tips
- Start with a modest λ (e.g., 0.1) and increase until validation loss stops improving.
- Prefer L2 for dense problems where all features contribute.
- Prefer L1 when you suspect only a few features are truly informative.
- Combine both (Elastic Net) for a balance of shrinkage and sparsity.