VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/implementing-l1-and-l2-regularization-using-sklearn/

⇱ Implementing L1 and L2 regularization using Sklearn - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementing L1 and L2 regularization using Sklearn

Last Updated : 31 Mar, 2026

Regularization is a technique used to prevent overfitting in machine learning models. It works by adding a penalty to the loss function so the model does not become too complex. The two most common types are L1 (Lasso) and L2 (Ridge) regularization. Now let us understand the concept in a simple flow:

  • We first control model complexity so it does not overfit the training data.
  • We then apply L1 regularization, which can reduce some feature coefficients to zero.
  • Next, we apply L2 regularization, which reduces coefficient values but keeps all features.
  • Finally, we choose the appropriate method depending on whether we want feature selection (L1) or coefficient shrinkage (L2).
👁 Undertanding-regularization2
Regularization

Implementation using Scikit Learn

Step 1: Import Required Libraries

  • Ridge : L2 Regularization
  • Lasso : L1 Regularization

Step 2: Load the Dataset

  • load_diabetes loads a built in regression dataset.
  • X contains input features.
  • y contains the target values.

Step 3: Split the Dataset

  • 80% of data is used for training and 20% for testing.
  • random_state=42 ensures reproducible results.

Step 4: Building model without Regularization

  • No penalty term is applied
  • Model may overfit if data is complex
  • This serves as our baseline model

Output:

👁 output200
Evaluating linear regression model

Step 5: Building model with L1 Regularization

  • alpha=0.7 controls penalty strength for L1.
  • mean_squared_error() calculates average squared prediction error.
  • r2_score() shows how well the model explains variation in the target variable.

Output:

👁 output201
Output

Step 6: Building model with L2 Regularization

  • alpha=1.0 controls regularization strength. Higher alpha means stronger penalty.
  • mean_squared_error() calculates average squared prediction error.
  • r2_score() shows how well the model explains variation in the target variable.

Output:

👁 Output
Evaluating ridge model

Download full code from here

Linear Regression vs. L2 vs. L1

Aspect

Linear Regression

Ridge (L2)

Lasso (L1)

Penalty Applied

No penalty

Squared terms

Absolute terms

Effect on Coefficients

No shrinkage

Shrinks coefficients

Can shrink to zero

Feature Selection

No

No

Yes

Overfitting Control

Low

Moderate

High

Comment