Skip to content
Middle+0 views

Could you explain L1 and L2 regularization in more detail?

Practice

Asked at

AI answer

L1 and L2 regularization add a penalty for the magnitude of the model coefficients to the loss function. For coefficients (w), this usually looks like:

  • L1: (L_{total}=L_{data}+\lambda\sum_j|w_j|). It can set some coefficients to zero, producing a sparse model, and is sometimes used for feature selection. When features are highly correlated, it may retain one of them almost arbitrarily.
  • L2: (L_{total}=L_{data}+\lambda\sum_jw_j^2). It smoothly shrinks coefficients, usually without setting them completely to zero. It is often more stable with correlated features and reduces the risk of overfitting.

(\lambda) determines the strength of regularization: a value that is too large can lead to underfitting. Features are usually scaled before regularization; otherwise, the penalty depends on their units of measurement. The intercept is usually not regularized. A combination of L1 and L2 is called Elastic Net.