CLASSICAL ML Β· INTERVIEW QUESTION
Explain regularization. Compare L1 (Lasso) and L2 (Ridge) regularization.
π‘ intermediate Β· Supervised Learning
Hint: L1 creates sparsity, L2 shrinks weights uniformly.
Answer
Regularization adds a penalty to the loss function to prevent overfitting by constraining model complexity.
L1 vs L2 Comparison
| L1 (Lasso) | L2 (Ridge) | |
|---|---|---|
| Penalty | $R = \sum | w_i |
| Effect | Drives weights to exactly zero | Shrinks weights toward zero |
| Result | Sparse model (feature selection) | All features kept, smaller weights |
| Geometry | Diamond constraint (sparse corners) | Circle constraint (no axis preference) |
| Best for | Many irrelevant features | All features potentially useful |
Elastic Net combines both:
python
from sklearn.linear_model import Lasso, Ridge, ElasticNet
# L1 regularization (sparse features)
lasso = Lasso(alpha=0.1)
# L2 regularization (multicollinearity)
ridge = Ridge(alpha=1.0)
# Elastic Net (grouped features)
enet = ElasticNet(alpha=0.1, l1_ratio=0.5)is chosen via cross-validation. Higher = stronger regularization = simpler model.
Related questions
- What is overfitting? How do you detect and prevent it?
- What is cross-validation? Compare k-fold, stratified, and time series CV.
- Explain precision, recall, F1-score, and when to use each.
- What is the ROC curve and AUC? What are their limitations?
- How do you handle class imbalance? Compare different approaches.