thebench.bio #1 Ecosystem for the Bio-Tech Industry
All Machine Learning questions

Question 12 of 20 Fundamentals Concept

How do L1 and L2 regularization differ, and when would you use each?

Short answer

Both add a penalty on coefficient size to reduce overfitting. L2, as in ridge, shrinks all coefficients smoothly and copes well with correlated features. L1, as in lasso, can set coefficients exactly to zero and so selects features, but among correlated features it tends to pick one arbitrarily.

Why

With many features and few samples, as in expression data, an unpenalized linear model can fit noise. Regularization adds a cost for large coefficients, controlled by a strength parameter, alpha in scikit-learn's Ridge and Lasso or C, its inverse, in LogisticRegression. The scikit-learn guide explains that ridge penalizes the sum of squared coefficients, which stabilizes estimates when features are nearly collinear.

Lasso penalizes the sum of absolute values, which produces sparse solutions with many coefficients exactly zero. That sparsity is attractive for a biomarker panel, because a model using 12 genes is cheaper to measure than one using 2,000. The catch is correlation.

If five genes in a pathway carry the same signal, lasso tends to keep one and drop the others, and which one can change between bootstrap samples. The guide notes that elastic net, a mix of both penalties, is useful when several features are correlated, because it tends to keep them together. In all cases features should be on comparable scales before penalizing, or the penalty falls unevenly.

The strength is a hyperparameter chosen by cross-validation, and the selected genes from lasso are a predictive choice, not evidence that the dropped genes are biologically irrelevant.

A strong answer covers

  • Both penalize coefficient size
  • L2 shrinks smoothly, handles collinearity
  • L1 gives sparse coefficients and selection
  • L1 picks arbitrarily among correlated features, elastic net helps
  • Scale features and tune strength by cross-validation

Common mistakes

  • Reading genes dropped by lasso as having no biological role
  • Applying a penalty to unscaled features so that units decide which coefficients shrink
  • Confusing C in LogisticRegression with alpha, where larger C means weaker regularization