Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Regularization

KTH JupyterHub Open In Colab Binder

Introduction to Regularization

While linear regression works well in many cases, it can suffer from overfitting when the model is flexible relative to the amount of data, for example when a high-degree polynomial is fitted to a few noisy data points. Overfitting occurs when a model captures not only the true underlying pattern but also random fluctuations or noise in the data. This leads to poor generalization on unseen data.

One way to combat overfitting is to apply regularization, which modifies the loss function by adding a penalty for large model parameters. Large parameters are a typical symptom of overfitting: they make the model react strongly to small changes in the input, and so to the noise in the training data. The penalty constrains the model’s flexibility and encourages simpler models that generalize better.

In this chapter, we will introduce two commonly used regularization techniques and their combination:

Ridge Regression (L2 Regularization)

In ridge regression, we modify the ordinary least squares loss function by adding a penalty proportional to the square of the parameters’ magnitudes. This penalty discourages large parameters, leading to a smoother model that is less likely to overfit.

The objective function for ridge regression is:

Lridge=∑i(f(xi)−yi)2+λ∑jβj2\mathcal{L}_{\text{ridge}} = \sum_i \left( f(\mathbf{x}_i) - y_i \right)^2 + \lambda \sum_j \beta_j^2

Where:

Ridge Regression by Explicit Loss Minimization

Here we implement the ridge loss explicitly and minimize it numerically, so that the penalty term stays visible. In practice you would normally reach for scikit-learn’s Ridge class. As data, we use 20 noisy points from a polynomial curve, and we fit a polynomial of degree 7 to them, with and without the penalty.

Notebook Cell
<Figure size 640x480 with 1 Axes>
Polynomial parameters (RSS): [ 5.47824138e-01 -1.59883737e+01  1.38372146e+02 -4.58078397e+02
  6.14035870e+02 -4.26836709e+01 -5.89981334e+02  3.56831336e+02]
Polynomial parameters (Ridge): [ 0.02248597  0.84009749  0.07809837 -0.27339012 -0.35827061 -0.33258107
 -0.27178283 -0.20743716]

In the code, alpha plays the role of λ\lambda.

LASSO Regression (L1 Regularization)

LASSO regression (Least Absolute Shrinkage and Selection Operator) is another regularization technique. Instead of penalizing the sum of squared parameters, LASSO penalizes the sum of the absolute values of the parameters.

The objective function for LASSO regression is:

Llasso=∑i(f(xi)−yi)2+λ∑j∣βj∣\mathcal{L}_{\text{lasso}} = \sum_i \left( f(\mathbf{x}_i) - y_i \right)^2 + \lambda \sum_j |\beta_j|

Where:

LASSO Regression by Explicit Loss Minimization

Here we implement the LASSO loss explicitly and minimize it numerically. Note that this general-purpose optimizer drives coefficients close to zero without setting them exactly to zero. Solvers designed for LASSO, such as the coordinate descent used by scikit-learn’s Lasso class or least-angle regression, do produce exact zeros.

<Figure size 640x480 with 1 Axes>
Polynomial parameters (RSS): [ 5.47824138e-01 -1.59883737e+01  1.38372146e+02 -4.58078397e+02
  6.14035870e+02 -4.26836709e+01 -5.89981334e+02  3.56831336e+02]
Polynomial parameters (LASSO): [-2.61105183e-03  1.01056480e+00 -3.25495973e-04 -1.95542003e-01
 -1.19779462e+00 -1.16530979e-02  2.19927836e-03 -2.58220514e-03]

Comparing Ridge and LASSO

Ridge regression shrinks all parameters but rarely sets any of them exactly to zero, so every feature stays in the model with a reduced influence. LASSO can shrink parameters all the way to zero and so performs feature selection. Ridge is therefore a natural choice when you believe that most features are relevant, and LASSO when you suspect that only a few of them are.

Elastic Net: Combining Ridge and LASSO

Elastic Net includes both the L1 and the L2 penalty:

Lelastic net=∑i(f(xi)−yi)2+λ1∑j∣βj∣+λ2∑jβj2\mathcal{L}_{\text{elastic net}} = \sum_i \left( f(\mathbf{x}_i) - y_i \right)^2 + \lambda_1 \sum_j |\beta_j| + \lambda_2 \sum_j \beta_j^2

Here, λ1\lambda_1 and λ2\lambda_2 set the strengths of the L1 and the L2 penalty. Elastic Net is particularly useful when features are correlated, as it encourages a grouping effect, where correlated features tend to get similar parameters.

Elastic Net Using Scikit-learn

scikit-learn’s ElasticNet class writes the penalty with an overall strength α\alpha (alpha) and a mixing parameter ρ\rho (l1_ratio), and it divides the squared errors by 2N2N. With NN data points, this corresponds to λ1=2Nαρ\lambda_1 = 2N\alpha\rho and λ2=Nα(1−ρ)\lambda_2 = N\alpha(1-\rho) in the formula above, so the settings below, with N=20N = 20, give λ1=0.1\lambda_1 = 0.1 and λ2=0.05\lambda_2 = 0.05. ElasticNet fits its own, unpenalized, intercept, so we leave the constant term out of the polynomial features.

<Figure size 640x480 with 1 Axes>

Why “L1” and “L2” Regularization?

The labels “L1” and “L2” come from the notation of vector norms in mathematics. A norm on a vector space is a function that assigns a non-negative length or size to each vector and satisfies certain axioms (positivity, homogeneity, triangle inequality). The L1 norm (also written ℓ1\ell_1-norm) of the parameter vector β=(β1,β2,…,βp)\boldsymbol{\beta} = (\beta_1, \beta_2, \dots, \beta_p) is

∥β∥1=∑j=1p∣βj∣.\|\boldsymbol{\beta}\|_1 = \sum_{j=1}^p |\beta_j|.

The L2 norm (also written ℓ2\ell_2-norm) is

∥β∥2=∑j=1pβj2,\|\boldsymbol{\beta}\|_2 = \sqrt{ \sum_{j=1}^p \beta_j^2 },

which corresponds geometrically to the Euclidean length of the vector. The LASSO penalty is thus λ∥β∥1\lambda \|\boldsymbol{\beta}\|_1, and the ridge penalty is λ∥β∥22\lambda \|\boldsymbol{\beta}\|_2^2, the squared L2 norm.

What you should know after reading this chapter

You should be able to:

  1. Explain what overfitting is, why a flexible model fitted to few data points tends to overfit, and why penalizing large parameters counteracts it.

  2. Write down the loss functions for ridge (L2), LASSO (L1) and elastic net regression. Explain what the regularization parameter λ\lambda does, including what happens at λ=0\lambda = 0 and as λ\lambda grows.

  3. Contrast ridge and LASSO: ridge shrinks all parameters, while LASSO can set some exactly to zero and so selects features. Argue which one suits a given problem.

  4. Explain where the names L1 and L2 come from, in terms of vector norms.