馃摌 L1 vs L2 Regularization: A Deep Interactive Guide
馃搼 Table of Contents
- Introduction
- Understanding Overfitting
- What is Regularization?
- L1 Regularization (Lasso)
- L2 Regularization (Ridge)
- Mathematical Explanation
- Key Differences
- Code Examples
- CLI Output
- Key Takeaways
- Related Articles
馃殌 Introduction
In machine learning, building a model that performs well on unseen data is the ultimate goal. However, models often become too complex and start memorizing training data instead of learning patterns.
⚠️ Understanding Overfitting
Overfitting occurs when a model captures noise along with the underlying pattern.
- High accuracy on training data
- Poor performance on test data
馃摉 Why does overfitting happen?
When models have too many parameters, they can perfectly fit training data—even random noise. This reduces their ability to generalize.
馃 What is Regularization?
Regularization is a technique used to reduce model complexity by penalizing large weights.
It modifies the loss function:
Loss = Original Loss + Penalty Term
馃敼 L1 Regularization (Lasso)
L1 adds a penalty based on absolute values of weights.
馃搻 Formula
L1 = 位 * 危 |wi|
馃幆 Effect
- Pushes weights to zero
- Performs feature selection
- Creates sparse models
馃摉 Deep Insight
L1 regularization creates sharp corners in optimization space, causing some coefficients to become exactly zero.
馃敼 L2 Regularization (Ridge)
L2 adds a penalty based on squared weights.
馃搻 Formula
L2 = 位 * 危 (wi²)
馃幆 Effect
- Shrinks weights smoothly
- Keeps all features
- Improves stability
馃摉 Deep Insight
L2 creates a smooth penalty surface, leading to balanced weight distribution instead of elimination.
馃搳 Mathematical Intuition
The full loss function becomes:
L = 危 (yi - 欧i)² + 位 * penalty
For L1:
L = 危 (yi - 欧i)² + 位 危 |wi|
For L2:
L = 危 (yi - 欧i)² + 位 危 (wi²)
馃摉 Why does this work?
Adding penalties discourages large weights, preventing the model from relying too heavily on specific features.
馃搻 Deep Mathematical Explanation
To truly understand regularization, we need to look at how it changes the optimization problem.
馃敼 Base Loss Function (Without Regularization)
L = 危 (yi - 欧i)²
This objective tries to minimize prediction error. However, it does not restrict model complexity.
馃敼 L1 Regularization (Lasso)
L = 危 (yi - 欧i)² + 位 危 |wi|
L1 adds a penalty proportional to the absolute values of weights.
- Encourages sparsity
- Creates sharp optimization boundaries
- Forces some weights exactly to zero
馃摉 Geometric Intuition
L1 regularization creates a diamond-shaped constraint region. The corners of this shape align with axes, which is why optimization often lands exactly on zero values for some weights.
馃敼 L2 Regularization (Ridge)
L = 危 (yi - 欧i)² + 位 危 (wi²)
L2 penalizes squared weights, leading to smoother optimization.
- Shrinks weights continuously
- No exact zero values
- Distributes importance across features
馃摉 Geometric Intuition
L2 creates a circular constraint region. Since there are no sharp corners, weights rarely become zero— they are just reduced proportionally.
馃敼 Gradient Perspective
During training, weights are updated using gradients.
L1 Gradient:
∂L/∂wi = error_gradient + 位 * sign(wi)
L2 Gradient:
∂L/∂wi = error_gradient + 2位wi
馃摉 Why This Matters
L1 applies a constant push toward zero, while L2 applies a proportional shrink. This is the key reason why L1 creates sparsity and L2 does not.
馃敼 Choosing Lambda (位)
- 位 = 0 → No regularization
- Small 位 → Slight control
- Large 位 → Strong simplification
⚖️ Key Differences
| Aspect | L1 (Lasso) | L2 (Ridge) |
|---|---|---|
| Sparsity | Yes | No |
| Feature Selection | Yes | No |
| Stability | Less stable | More stable |
| Best Use Case | High-dimensional data | General purpose |
馃捇 Code Example
from sklearn.linear_model import Lasso, Ridge # L1 Regularization lasso = Lasso(alpha=0.1) lasso.fit(X, y) # L2 Regularization ridge = Ridge(alpha=0.1) ridge.fit(X, y)
馃枼 CLI Output Sample
Training Model... Epoch 1/5 Loss: 12.45 Epoch 5/5 Loss: 4.32 L1 Weights: [0.0, 1.2, 0.0, 3.4] L2 Weights: [0.5, 1.1, 0.8, 2.9]
馃搨 Expand Explanation
Notice how L1 forces some weights to zero, while L2 keeps all weights but reduces their magnitude.
馃幆 Key Takeaways
- L1 = Feature Selection
- L2 = Weight Shrinking
- Both reduce overfitting
- Lambda controls penalty strength
馃搶 Final Thoughts
Regularization is essential for building robust machine learning models. Choosing between L1 and L2 depends on your problem, data size, and feature characteristics.
Mastering these techniques ensures your models perform well not just in training—but in the real world.