Regularization: Ridge, Lasso and ElasticNet
The Problem of Overfitting
When we fit a model to training data, we want it to generalize well to unseen data. A model that memorizes training data but fails on test data is said to overfit.
Bias-Variance Tradeoff
The fundamental tension in supervised learning:
| Source | Description | Effect on Test Error |
|---|---|---|
| Bias | Error from wrong assumptions | High → underfitting |
| Variance | Error from sensitivity to training data | High → overfitting |
| Irreducible | Noise in the data | Cannot reduce |
As model complexity increases:
- Bias decreases (model captures more patterns)
- Variance increases (model becomes more sensitive)
- Total error follows a U-shaped curve
Why Regularization?
Regularization addresses overfitting by penalizing model complexity. Instead of minimizing only the loss function, we minimize:
where:
- is the regularization strength (hyperparameter)
- is the regularization term (penalty function)
- are the model parameters
Key insight: Regularization trades training performance for generalization.
Ridge Regression (L2 Regularization)
Formulation
Ridge regression adds an L2 penalty (squared magnitude of coefficients):
Expanding the penalty term:
Closed-Form Solution
Taking the derivative and setting to zero:
Key properties:
- Always has a unique solution (matrix is always invertible for )
- Coefficients are shrunk toward zero but never exactly zero
- Equivalent to OLS with modified covariance matrix
Geometric Interpretation
The Ridge solution is where the elliptical contours of the OLS loss meet the circular L2 constraint. Since circles are smooth, the intersection rarely occurs exactly on an axis.
Lasso Regression (L1 Regularization)
Formulation
Lasso (Least Absolute Shrinkage and Selection Operator) adds an L1 penalty (absolute magnitude):
Expanding:
No Closed-Form Solution
Unlike Ridge, Lasso has no analytical solution due to the non-differentiable absolute value. We use:
- Coordinate descent
- Proximal gradient methods
- Subgradient methods
Sparsity Property
The L1 penalty induces sparsity — it drives some coefficients to exactly zero:
where is the soft-thresholding operator.
Why does L1 produce sparsity?
The L1 constraint region has corners on the axes. The elliptical contours of OLS loss are more likely to intersect at these corners, yielding solutions where some .
ElasticNet: Best of Both Worlds
Formulation
ElasticNet combines L1 and L2 penalties:
Using the mixing parameter :
| Value | Behavior |
|---|---|
| Pure Ridge (L2 only) | |
| Pure Lasso (L1 only) | |
| ElasticNet |
Advantages over Lasso
- Grouped selection: When features are correlated, Lasso selects one arbitrarily; ElasticNet selects the group
- Smooth penalty: Differentiable everywhere (unlike L1)
- More stable: Less sensitive to small changes in data
- : Works when number of features exceeds samples
Coefficient Shrinkage Visualization
Regularization Path
The regularization path shows how coefficients change as varies:
Key Observations
- Left side ( small): All features included, model close to OLS
- Right side ( large): Most coefficients zero, simple model
- Sparsity: Lasso drives coefficients to exactly zero sequentially
- : Value that minimizes cross-validation error
- : Largest within 1 SE of (sparser model)
Choosing Lambda: Cross-Validation
We select using k-fold cross-validation:
Common Selection Strategies
| Strategy | Description | When to Use |
|---|---|---|
| Minimizes CV error | Maximum predictive power | |
| Largest within 1 SE of | Simpler, more interpretable model |
Implementation in Python
Basic Setup
Ridge Regression
Lasso Regression
ElasticNet
Visualizing the Regularization Path
Comparing Models
When to Use Each Method
| Scenario | Recommended Method | Reason |
|---|---|---|
| Many small effects | Ridge | Keeps all features, reduces magnitude |
| Few strong predictors | Lasso | Automatic feature selection |
| Correlated features | ElasticNet | Grouped selection, stability |
| High-dimensional () | ElasticNet | Handles collinearity, selects features |
| Interpretability needed | Lasso | Sparse model |
| Maximum accuracy needed | Ridge/ElasticNet | Depends on data structure |
Practical Guidelines
1. Always Standardize
2. Start with Cross-Validation
3. Examine the Regularization Path
4. Compare with OLS
Summary
| Property | Ridge (L2) | Lasso (L1) | ElasticNet |
|---|---|---|---|
| Penalty | |||
| Sparsity | No | Yes | Yes |
| Feature Selection | No | Yes | Yes |
| Correlated Features | Keeps all | Selects one | Selects group |
| Solution | Closed-form | Iterative | Iterative |
| When to Use | Many small effects | Few strong predictors | Mixed scenarios |
Key Takeaways:
- Regularization prevents overfitting by penalizing model complexity
- Ridge shrinks coefficients but keeps all features
- Lasso performs automatic feature selection via sparsity
- ElasticNet combines both benefits, often the best choice
- Always use cross-validation to select the regularization strength
- Standardize features before applying regularization