Regularization (L1/L2) — ML Technical Interview Questions & Answers
Penalizing large weights to reduce overfitting.
Regularization (L1/L2)
- Simple meaning: Regularisation stops weights from going wild so the model cannot memorise noise. L2 (Ridge) shrinks weights smoothly. L1 (Lasso) can push some weights to exactly 0 (feature drop).
- Picture: A polynomial that wiggles through every student. Add a penalty for large coefficients. The curve calms down. Test score usually improves if you were overfitting.
- sklearn: Ridge, Lasso, LogisticRegression(penalty=...). Strength is alpha / C — know which way “more regularisation” goes for the class you use.
- Trap: regularising when you are underfitting (you will get worse). Trap: mixing up L1 and L2 in a viva — L1 sparsifies, L2 shrinks.
Technical Interview Tip: L2 shrinks. L1 can set weights to 0. Use when train ≫ test.
Code Examples for Regularization (L1/L2)
Regularization (L1/L2) — working example
# Toy train vs test errors (higher = worse)
models = [
{"name": "underfit", "train": 0.30, "test": 0.32},
{"name": "good", "train": 0.08, "test": 0.10},
{"name": "overfit", "train": 0.01, "test": 0.28},
]
for m in models:
gap = m["test"] - m["train"]
print(m["name"], "gap=", round(gap, 2))
# L2 idea: penalty += lambda * w**2 (discourages huge weights)Regularization (L1/L2) — overfit = low train / high test gap; underfit = high error everywhere; regularization limits capacity.
How to Explain Regularization (L1/L2) to an Interviewer
L1 vs L2?
L1 can zero weights L2 shrinks all
When regularise?
Overfit Train much better than test