What is Lasso Regression (original) (raw)

Last Updated : 10 Feb, 2026

Lasso Regression (Least Absolute Shrinkage and Selection Operator) is a linear regression technique with L1 regularization that improves model generalization by adding a penalty. This penalty not only controls overfitting but also performs automatic feature selection by shrinking some coefficients exactly to zero, making Lasso useful in high-dimensional datasets where interpretability and sparsity are important.

**Note: L1 regularization adds the sum of absolute values of model weights to the loss function, encouraging sparsity by pushing some weights exactly to zero.

Bias–Variance Tradeoff

The Bias–Variance Tradeoff describes the balance between a model’s simplicity and its ability to generalize to unseen data. In machine learning, reducing bias often increases variance and vice versa, so finding the right balance is important for building accurate models.

Lasso Regression directly influences the bias–variance tradeoff through its regularization strength (λ or α).

mse_bias_varience

Bias Variance Tradeoff

**Low λ (weak regularization)

**High λ (strong regularization)

**How Lasso balances this tradeoff

Working of Lasso Regression

Let's see the working:

1. Base Linear Model

Lasso begins with a standard linear regression setup where the target variable is predicted as a weighted sum of input features. Without regularization, this model can overfit when the number of predictors is large.

2. L1-Regularized Objective Function

\sum_{i=1}^{n} (y_i - \hat{y}_i)^2 + \lambda \sum_{j=1}^{p} |w_j|

3. Coefficient Shrinkage

4. Embedded Feature Selection

5. Optimization Strategy

Implementation

Let's implement it in python:

Step 1: Import Libraries

We need to import the necessary libraries such as NumPy, Matplotlib and sklearn.

Python `

import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_regression from sklearn.model_selection import train_test_split from sklearn.linear_model import Lasso from sklearn.metrics import mean_squared_error, r2_score

`

Step 2: Sample Dataset

Here we:

X, y = make_regression( n_samples=500, n_features=10, n_informative=5, noise=15, random_state=42 )

`

Step 3: Split Data into Training and Testing Sets

X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 )

`

Step 4: Train the Lasso Regression Model

lasso = Lasso(alpha=0.1) lasso.fit(X_train, y_train)

`

**Output:

Screenshot-2026-02-10-175612

Model Training

Step 5: Make Predictions and Evaluate Performance

y_pred = lasso.predict(X_test)

mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred)

mse, r2

`

**Output:

(29.280987296173713, 0.9933960650641204)

Step 6: Regression Line

y_pred = lasso.predict(X_test)

plt.figure(figsize=(6, 6)) plt.scatter(y_test, y_pred)

min_val = min(y_test.min(), y_pred.min()) max_val = max(y_test.max(), y_pred.max()) plt.plot([min_val, max_val], [min_val, max_val])

plt.xlabel("Actual Values") plt.ylabel("Predicted Values") plt.title("Actual vs Predicted (Lasso Regression)") plt.show()

`

**Output:

download-

Plot

Step 7: Plot Lasso Coefficients (Feature Selection)

plt.figure(figsize=(8, 4)) plt.bar(range(len(lasso.coef_)), lasso.coef_, color="green") plt.xlabel("Feature Index") plt.ylabel("Coefficient Value") plt.title("Lasso Regression Coefficients") plt.show()

`

**Output:

download

Plot

It shows which feature is more important for feature selection.

Step 8: Plot Effect of Regularization Strength (Lasso Path)

alphas = np.logspace(-2, 1, 20) coefficients = []

for a in alphas: model = Lasso(alpha=a) model.fit(X_train, y_train) coefficients.append(model.coef_)

coefficients = np.array(coefficients)

plt.figure(figsize=(8, 5)) for i in range(coefficients.shape[1]): plt.plot(alphas, coefficients[:, i]) plt.xscale("log") plt.xlabel("Alpha (λ)") plt.ylabel("Coefficient Value") plt.title("Lasso Path: Coefficients vs Regularization Strength") plt.show()

`

**Output:

download

Result

Ridge Regression vs Lasso Regression

Let's see the comparison between ridge regression and lasso regression:

Aspect Ridge Regression Lasso Regression
Regularization L2 penalty L1 penalty
Feature selection It does not supports feature selection It supports feature selection
Coefficient shrinkage Smooth shrinkage Aggressive shrinkage
Zero coefficients Never Possible
Model sparsity Dense Sparse
Multicollinearity Handles very well Moderate handling
Interpretability Lower Higher

Applications

Advantages

Limitations