Skip to main content
LearnMLAcademy
Intermediate10 min read

Lasso Regression

Perform automated feature selection using Lasso L1 regularization sparsity, with Python code, architecture diagrams, worked examples, and interview quiz.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Lasso Regression

Lasso Regression is a regularization technique used to improve prediction accuracy and implicitly perform feature selection.

The full form of LASSO is:

Least Absolute Shrinkage and Selection Operator

Lasso Regression is based on L1 Regularization.

Lasso Regression in Simple Words

Lasso starts with Linear Regression, but adds a rule that discourages unnecessarily large coefficients. With enough regularization, some coefficients can become exactly zero, so those features stop affecting the prediction.

1

Start with Features

Use the available input columns.

2

Fit a Linear Model

Learn one coefficient for each feature.

3

Add L1 Penalty

Penalize the absolute size of coefficients.

4

Shrink Some to Zero

Some features may drop out of the model.

A Tiny House-Price Example

Suppose a model starts with four numerical features. After Lasso, the coefficients might look like this:

FeatureBefore LassoAfter Lasso
House Size8.05.4
Bedrooms3.21.7
Wall Color Code0.80
Random ID Number-0.50

Important: this is an illustration, not a promise that Lasso will always choose the same features. The result depends on the data and the regularization strength.

Why Lasso Regression is Needed

Machine learning models often suffer from overfitting, too many irrelevant features, complex models, multicollinearity, and poor generalization.

Ordinary Linear Regression estimates a coefficient for every supplied feature. If many predictors are weak, noisy, or highly correlated, the model can become harder to interpret and may generalize poorly. Lasso can help by:

  • Penalizing large coefficient magnitudes
  • Shrinking coefficients
  • Allowing some coefficients to become exactly zero
  • Simplifying the model

Beginner Intuition

Imagine you are packing a travel bag.

  • You cannot carry everything. So you keep only the most important items.
  • Lasso Regression works similarly: it encourages a simpler model by shrinking coefficients, and some may become zero. Which features remain depends on the data and the chosen penalty.

Real-World Example

Suppose we want to predict house prices using: House Size, Location, Bedrooms, Bathrooms, Wall Color, Owner Name, Garden Plants.

Not every feature will necessarily add useful predictive information. Lasso can shrink weak coefficients and, for some settings, make some of them exactly zero.

It may keep:
  • House Size
  • Location
  • Bedrooms
And remove:
  • Wall Color
  • Garden Plants
  • Owner Name

This can simplify the model and may improve generalization, but it should be validated on unseen data.

Visual Understanding of Overfitting

GOOD GENERALIZATION

Training ErrorLow
Validation/Test ErrorLow

OVERFITTING

Training ErrorVery Low
Validation/Test ErrorHigh

Hierarchy of Regression Algorithms

Regression Algorithms
Linear Regression
Regularized Regression
Ridge (L2)
Lasso (L1)
Elastic Net

Linear Regression Recap & Problem

Basic Equation:

y = b₀ + b₁x₁ + b₂x₂ + ... + bₙxₙ

Worked Example:

  • b₀ = 5, b₁ = 3, x = 4
  • y = 5 + 3(4)
  • y = 17

Problem with Linear Regression:

With many correlated or noisy predictors, ordinary Linear Regression can produce unstable coefficients and may overfit. Coefficient size also depends on the scale of each feature.

Example: y = 5 + 700x₁ + 1200x₂
A large number alone is not proof of instability; always consider feature scale.

Core Idea of Lasso Regression

Lasso Regression adds a penalty to the Linear Regression cost function. This penalty shrinks coefficients, and for suitable regularization some coefficients can become exactly zero. A zero coefficient means that feature no longer contributes to the linear prediction.

Lasso Cost Function:

Cost = Prediction Loss + λ × Σ|w|

This is a teaching form. Libraries may scale the loss differently; the core idea is the same: data-fit loss plus an L1 penalty on the feature coefficients.

Part 1: Prediction Error
Σ(y - ŷ)²

Measures prediction mistakes.

Part 2: L1 Penalty
λ Σ|θ|

Penalizes coefficient size.

Understanding the L1 Penalty

The L1 penalty uses the absolute value of coefficients.

  • Suppose: θ₁ = 3, θ₂ = -5

Step 1 — Absolute values: |3| + |-5| = 3 + 5 = 8

Step 2 — Choose λ = 2: L1 penalty = 2 × 8 = 16

Step 3 — If prediction loss = 20: Total cost = 20 + 16 = 36

Increasing λ makes coefficient size more expensive, so the fitted model is pushed toward smaller—and sometimes zero—coefficients.

Understanding Lambda (λ)

Lambda controls regularization strength.

Lambda ValueEffect
λ = 0Normal Linear Regression.
Small λSmall regularization (Weak Penalty, Complex Model).
Medium λModerate regularization; whether it is a good choice depends on validation results.
Very large λUnderfitting (Strong Penalty, Simpler Model).

How Lasso Performs Feature Selection

A distinctive property of Lasso is that it can produce a sparse solution: some coefficients may become exactly zero.

Before Lasso
x₁ = 5
x₂ = 8
x₃ = 3
x₄ = 7
After Lasso
x₁ = 5
x₂ = 0
x₃ = 0
x₄ = 7

In this illustration, x₂ and x₃ become zero. The real features selected by Lasso depend on the training data and the chosen regularization strength.

Coefficient Shrinking Visualization

Why Lasso Creates Sparse Models

With two coefficients, the L1 constraint has a diamond-shaped boundary. Its sharp corners lie on the axes, which helps explain why Lasso solutions often land at points where one or more coefficients are exactly zero.

      /\
     /  \
    /    \
    \    /
     \  /
      \/

Ridge vs Lasso Penalty

Ridge Penalty (L2)

λΣθ²

Shrinks coefficients but rarely reaches zero.

Lasso Penalty (L1)

λΣ|θ|

Shrinks coefficients and can make them zero.

FeatureRidgeLasso
Regularization TypeL2L1
Can create exact-zero coefficientsTypically noYes
Promotes sparsityNoYes
Useful for feature selectionNot directlyOften
Highly correlated featuresOften keeps them togetherMay select one and zero others
InterpretabilityKeeps all coefficientsCan be simpler when sparse

Increasing L1 regularization usually increases bias and can reduce variance. The useful tradeoff depends on validation performance.

Bias-Variance Tradeoff based on Lambda (λ)

Multicollinearity in Lasso

What is Multicollinearity?

When features are highly correlated (e.g., House Area and Number of Rooms).

Lasso Behavior with Correlated Features:

With strongly correlated predictors, Lasso may keep one and shrink another to zero. Which one is selected can be unstable when predictors carry very similar information.

  • Feature A → Kept
  • Feature B → Removed

Cost Function Optimization

Lasso can be optimized in different ways. Scikit-learn's Lasso estimator uses coordinate descent. Gradient-based thinking is still useful for intuition, but the absolute-value penalty has a sharp point at zero, so its update is not simply “ordinary gradient + λ” for every coefficient.

Conceptual Gradient-Style Update (for intuition)

w_new = w - η(gradient_loss + λ × sign(w))

Worked Example

  • Current Weight (θ): 10
  • Learning Rate (η): 0.1
  • Gradient (∂J/∂θ): 4
  • Lambda (λ): 1

Step 1: sign(10) = +1

Step 2: combined gradient = 4 + (1 × 1) = 5

Step 3: new weight = 10 - (0.1 × 5) = 9.5

The L1 term adds pressure toward zero. This is a teaching calculation; practical Lasso solvers such as scikit-learn's coordinate-descent solver handle the non-smooth point at zero directly.

Why Feature Scaling is Important

Feature scale matters because coefficient magnitudes depend on the units of the inputs. If one feature is measured in rupees and another in years, the same L1 penalty can affect them very differently. Standardizing numerical features often makes the regularization easier to compare across features.

X_scaled = (X - μ) / σ

// Example scaling calculation

Suppose: X = 50, Mean(μ) = 40, Std Dev(σ) = 5

X_scaled = (50 - 40) / 5

X_scaled = 10 / 5

X_scaled = 2

Python Implementation using Scikit-Learn

Step 1: Import Libraries
import numpy as np
import pandas as pd
from sklearn.linear_model import Lasso, LassoCV
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
Step 2 & 3: Create & Split Data
# Column 1: Study hours (useful)
# Column 2: Locker number pattern (not useful here)
X = np.array([
    [1, 0], [2, 1], [3, 0], [4, 1], [5, 0],
    [6, 1], [7, 0], [8, 1], [9, 0], [10, 1]
], dtype=float)
Y = np.array([8, 11, 14, 17, 20, 23, 26, 29, 32, 35], dtype=float)

X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.2, random_state=42
)
Step 4: Scale Features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Step 5 & 6: Train & Predict
model = Lasso(alpha=0.5, max_iter=10000)
model.fit(X_train_scaled, Y_train)

predictions = model.predict(X_test_scaled)
print("Coefficients:", np.round(model.coef_, 3))
print("Predictions:", np.round(predictions, 2))
Complete Assembled Code
import numpy as np
import pandas as pd
from sklearn.linear_model import Lasso, LassoCV
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Column 1: Study hours (useful)
# Column 2: Locker number pattern (not useful here)
X = np.array([
    [1, 0], [2, 1], [3, 0], [4, 1], [5, 0],
    [6, 1], [7, 0], [8, 1], [9, 0], [10, 1]
], dtype=float)
Y = np.array([8, 11, 14, 17, 20, 23, 26, 29, 32, 35], dtype=float)

X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.2, random_state=42
)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

model = Lasso(alpha=0.5, max_iter=10000)
model.fit(X_train_scaled, Y_train)
predictions = model.predict(X_test_scaled)

print("Coefficients:", np.round(model.coef_, 3))
print("Predictions:", np.round(predictions, 2))

# For real projects, choose alpha with cross-validation
cv_model = LassoCV(cv=5, random_state=42, max_iter=10000)
cv_model.fit(X_train_scaled, Y_train)
print("Chosen alpha:", cv_model.alpha_)

What should you notice?

Coefficients: [7.578 0.000]

The first feature keeps a non-zero coefficient, while the second becomes zero in this tiny example. That is the sparse behaviour Lasso is known for.

The exact values depend on the train/test split, scaling and alpha. For real work, tune alpha using cross-validation rather than assuming one value is always best.


Advantages of Lasso

  • Can produce sparse models with some exact-zero coefficients.
  • Can reduce overfitting by adding regularization.
  • Simplifies models for easier interpretation.
  • Can simplify interpretation when many coefficients become zero.

Disadvantages of Lasso

  • Sensitive to scaling.
  • Can sometimes remove useful features with excessive regularization.
  • With highly correlated predictors, the selected feature can be unstable.

Lasso vs Ridge vs Elastic Net

FeatureRidgeLassoElastic Net
RegularizationL2L1L1 + L2
Can create exact-zero coefficientsTypically noYesYes
Promotes sparsityNoYesYes
Correlated featuresOften stableMay select oneCan balance L1 + L2
ComplexityMediumMediumHigher

Industry Applications Tree

Healthcare
  • Disease Prediction
  • Gene Selection
  • Medical Diagnostics
Finance
  • Risk Analysis
  • Credit Scoring
  • Fraud Detection
Marketing
  • Customer Analytics
  • Sales Forecasting
  • Advertisement Analysis

End-to-End Lasso Regression Workflow

Collect Dataset
↓
Clean Data
↓
Scale Features
↓
Train Lasso Model
↓
Apply L1 Penalty
↓
Some Coefficients May Become Zero
↓
Generate Predictions
↓
Evaluate Performance

Common Questions About Lasso Regression

Does Lasso always remove features?

No. Some coefficients become zero only when the data and regularization strength lead to a sparse solution.

Is a bigger alpha always better?

No. Too much regularization can underfit. Choose alpha using validation or cross-validation.

Why should numerical features often be scaled?

Because L1 regularization acts on coefficient magnitudes, and those magnitudes depend on feature units.

What happens with correlated features?

Lasso may keep one correlated predictor and shrink another to zero. Elastic Net can be more stable when groups of predictors are strongly correlated.

Continue Learning

Use these lessons to connect Lasso with the ideas around it:

Final Summary

Lasso Regression is a powerful machine learning regularization technique that improves Linear Regression by penalizing large coefficients using the absolute value of weights (L1 Penalty).

Its most distinctive behaviour is sparsity: for suitable regularization, some coefficients can become exactly zero. This can simplify a model and act as an embedded feature-selection mechanism, but the selected features still need validation.

Most Important Insight to Remember:

"Ridge mainly shrinks coefficients, while Lasso can shrink some coefficients all the way to zero. That makes Lasso useful for sparse models and embedded feature selection—but the chosen features still depend on the data and alpha."

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Lasso Regression"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Summary / Key Takeaways

  • Lasso adds an L1 penalty based on absolute coefficient magnitudes.
  • L1 can drive some coefficients exactly to zero.
  • Scaling matters because the penalty acts on coefficient size.
  • With correlated features, selection can be unstable.
  • Too much penalty removes signal; too little gives little shrinkage.
  • Tune the penalty and inspect selection stability.

Knowledge Check

Progress: 0/4

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 4
What makes Lasso's L1 penalty produce exactly zero coefficients while Ridge's L2 only shrinks them close to zero?
Question 2 of 4
A dataset has 800 features and domain experts believe only 15-20 are genuinely predictive. Which regularisation is most appropriate?
Question 3 of 4
Lasso is applied to a genomics dataset where 50 genes are highly co-expressed. What will Lasso likely do?
Question 4 of 4
At λ→0 and λ→∞, what does a Lasso model predict?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1What makes Lasso's L1 penalty produce exactly zero coefficients while Ridge's L2 only shrinks them close to zero?▼

Correct Answer: The L1 constraint region is diamond-shaped with corners on the axes. Optimal solutions typically land at corners where one or more coordinates are exactly zero. The L2 ball has no corners — the intersection always occurs at a point with all coordinates nonzero

This geometric intuition explains why L1 and L2 have such different behaviour. The diamond's corners create attractors where coefficients land at zero; the circle has no such attractors.

2A dataset has 800 features and domain experts believe only 15-20 are genuinely predictive. Which regularisation is most appropriate?▼

Correct Answer: Lasso — it will drive the ~780 irrelevant features to exactly zero during training, producing a sparse, interpretable model that domain experts can validate against their prior knowledge

This is Lasso's primary use case: high-dimensional data where sparsity is expected. After training, you can show domain experts a list of 15 features with nonzero weights.

3Lasso is applied to a genomics dataset where 50 genes are highly co-expressed. What will Lasso likely do?▼

Correct Answer: Lasso arbitrarily selects one gene from the co-expressed group and zeros out the other 49 — even though they are all equally informative. Ridge retains all 50 with small weights; Elastic Net selects a subset of the group

Lasso's arbitrary selection among correlated features is a known weakness. Elastic Net (or Group Lasso) is preferred when correlated features should be handled as a group.

4At λ→0 and λ→∞, what does a Lasso model predict?▼

Correct Answer: At λ→0, Lasso approaches standard OLS with all features nonzero. At λ→∞, all coefficients are forced to zero and the model predicts the training mean for every input. Use cross-validation (LassoCV) to find the optimal λ between these extremes

LassoCV efficiently searches over a path of λ values using warm starting. The two extremes illustrate the full regularisation spectrum: from pure OLS to 'ignore all features.'

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Optional Learning Resources

These resources may complement the lesson. They are optional and are not required to use ML Academy.

As an Amazon Associate I earn from qualifying purchases. Other links are identified separately when they are not affiliate links.

Next