Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Machine Learning›Regularization — L1 (Lasso) and L2 (Ridge)

Model Training Deep Dive

Regularization — L1 (Lasso) and L2 (Ridge)

Regularization is a mathematical technique used to prevent overfitting by penalizing complex model parameters during training. By adding a constraint to the objective function, it encourages the model to prefer simpler solutions that generalize better to unseen data. It is the primary tool for balancing the bias-variance tradeoff when your model shows high variance on validation sets.

The Problem: Overfitting and High Variance

Overfitting occurs when a model learns to map input features to output targets by memorizing the noise present in the training set rather than learning the underlying signal. In high-dimensional spaces, a model has enough degrees of freedom to pass through almost every training point exactly, leading to massive weights that fluctuate wildly to accommodate small variances. When we deploy such a model, it fails because it lacks the ability to generalize, acting like an expert who memorized an answer key rather than understanding the subject matter. Mathematically, this is characterized by extremely large coefficient values. Regularization solves this by modifying the objective function, adding a penalty term that discourages the optimization process from letting weights grow arbitrarily large. This forces the model to find a compromise between fitting the data and keeping parameters small, effectively smoothing the decision boundary and reducing the model's sensitivity to small perturbations in the training data.

# Simulate a noisy dataset to demonstrate overfitting
import numpy as np
from sklearn.linear_model import LinearRegression

# Generate random synthetic data
X = np.random.rand(20, 1)
y = 3 * X + np.random.normal(0, 0.1, (20, 1))

# A simple model will likely capture noise if we had many features
model = LinearRegression()
model.fit(X, y)
print(f"Coefficients without regularization: {model.coef_}")

L2 Regularization (Ridge Regression)

L2 regularization, often referred to as Ridge regression, adds the sum of squared weights multiplied by a penalty factor, lambda, to the loss function. When the optimizer minimizes this combined cost, it faces a tug-of-war between reducing the error on the training data and keeping the weights as close to zero as possible. Because the penalty is quadratic, it penalizes larger weights exponentially more than smaller ones. This forces the model to distribute the learning responsibility across all input features rather than relying too heavily on any single one. As a result, Ridge regression shrinks all coefficients toward zero but never forces them to be exactly zero. This is incredibly beneficial when you have many features that are correlated, as it preserves all inputs while preventing any single one from causing high variance. The outcome is a stable, consistent model that performs reliably even when the test data distribution shifts slightly from the training distribution.

from sklearn.linear_model import Ridge

# Ridge adds an L2 penalty to the loss
# alpha is the strength of regularization
ridge_model = Ridge(alpha=1.0)
ridge_model.fit(X, y)

# Weights are small and distributed
print(f"Ridge coefficients: {ridge_model.coef_}")

L1 Regularization (Lasso Regression)

L1 regularization, known as Lasso (Least Absolute Shrinkage and Selection Operator), adds the sum of the absolute values of the weights to the objective function. Unlike the smooth, quadratic nature of L2, the absolute value function creates a sharp 'corner' at zero. During gradient descent, this geometry makes it much more likely for the optimizer to drive individual coefficients all the way to zero. Because of this behavior, Lasso acts as both a regularizer and a feature selector; it effectively identifies and removes irrelevant input features by assigning them zero-weight. This is highly useful in scenarios where you suspect that only a subset of your input features actually contributes to the target output, such as in genomic data or high-dimensional sensor readings. By zeroing out the noise-inducing features, Lasso produces a sparse model that is not only robust to overfitting but also significantly more interpretable for downstream analysis, as it highlights only the most impactful variables.

from sklearn.linear_model import Lasso

# Lasso adds an L1 penalty to the loss
# It produces sparse results
lasso_model = Lasso(alpha=0.1)
lasso_model.fit(X, y)

# Some coefficients might be exactly 0
print(f"Lasso coefficients: {lasso_model.coef_}")

Choosing Between Ridge and Lasso

Deciding between Ridge and Lasso depends primarily on your objective regarding the model's feature set. Use Ridge regression when you have a high degree of collinearity among features or when you believe that all your input features contribute some predictive power. Because Ridge retains all features, it preserves the holistic relationship between inputs and outputs, which is vital when you cannot afford to discard information. Conversely, use Lasso when you have a very large number of features relative to the number of training samples, or when you strongly suspect that many of your features are redundant or pure noise. The sparsification provided by Lasso simplifies the model, making it much easier to deploy and faster to execute during inference. If you find that neither is perfectly suited, you can utilize Elastic Net, which blends both L1 and L2 penalties, allowing you to control both the sparsity and the stability of the final coefficient estimates through two separate hyperparameter tuning variables.

from sklearn.linear_model import ElasticNet

# ElasticNet combines L1 and L2
# l1_ratio=0.5 means a 50/50 split
en_model = ElasticNet(alpha=0.1, l1_ratio=0.5)
en_model.fit(X, y)
print(f"ElasticNet coefficients: {en_model.coef_}")

Hyperparameter Tuning with Cross-Validation

Regardless of whether you choose Ridge or Lasso, the final performance of your model hinges on the selection of the regularization strength, typically denoted as alpha. An alpha that is too low provides insufficient constraint, leading to the original overfitting problem you set out to solve, while an alpha that is too high forces the model to underfit, causing it to ignore even the most important patterns in the training data. The gold standard for selecting this parameter is k-fold cross-validation, where you split your data into multiple chunks and systematically train on k-1 chunks while validating on the remaining one for various alpha values. By averaging the performance across these iterations, you obtain a statistically robust measure of how the model generalizes. This process transforms regularization from a manual guess into a data-driven optimization, ensuring that your penalty strength is perfectly calibrated to the specific signal-to-noise ratio inherent in your unique dataset and problem domain.

from sklearn.linear_model import RidgeCV

# RidgeCV performs cross-validation to find best alpha
# We provide a list of values to test
alphas = [0.01, 0.1, 1.0, 10.0]
ridge_cv = RidgeCV(alphas=alphas)
ridge_cv.fit(X, y)

print(f"Optimal alpha found: {ridge_cv.alpha_}")

Key points

  • Overfitting occurs when model weights become excessively large, capturing training noise instead of general signal.
  • L2 regularization penalizes the sum of squared weights, shrinking them towards zero without eliminating them.
  • Ridge regression is ideal when multiple features are correlated and you wish to retain all of them.
  • L1 regularization penalizes the sum of absolute weights, which facilitates the selection of features by driving many to zero.
  • Lasso regression is the preferred choice when you have many irrelevant features and require a sparse, interpretable model.
  • The alpha hyperparameter controls the balance between minimizing training loss and minimizing the penalty term.
  • Cross-validation is essential for identifying the optimal alpha value that achieves the best generalization performance.
  • Elastic Net provides a hybrid approach by combining both L1 and L2 penalties for complex regularization needs.

Common mistakes

  • Mistake: Thinking L1 regularization always leads to a better model than L2. Why it's wrong: L1 is for feature selection, but it can perform worse than L2 if features are highly correlated or if the true model is dense. Fix: Use Ridge (L2) when you suspect most features contribute to the target, and Lasso (L1) when you want to enforce sparsity.
  • Mistake: Neglecting to scale features before applying regularization. Why it's wrong: Regularization terms penalize coefficient magnitudes; if features are on different scales, the penalty will unfairly suppress variables with smaller original units. Fix: Always apply standard scaling (centering and scaling) to features before fitting a regularized model.
  • Mistake: Treating the regularization parameter (lambda/alpha) as a fixed value regardless of dataset size. Why it's wrong: The optimal amount of regularization depends on the variance-bias trade-off specific to the training sample size and complexity. Fix: Always use cross-validation to tune the hyperparameter alpha for every specific dataset.
  • Mistake: Assuming L1 regularization eliminates collinearity. Why it's wrong: When features are highly correlated, L1 arbitrarily picks one and discards others, which can lead to instability in feature selection. Fix: Use Elastic Net, which combines L1 and L2, to handle groups of correlated features more gracefully.
  • Mistake: Believing that regularization is only necessary for linear regression. Why it's wrong: Overfitting is a universal property of high-capacity models, not just linear ones. Fix: Regularization should be applied to any parametric model prone to overfitting, including logistic regression and deep neural networks.

Interview questions

What is the primary purpose of regularization in machine learning?

Regularization is a technique used to prevent overfitting in machine learning models by adding a penalty term to the loss function. When a model becomes too complex, it starts learning the noise in the training data rather than the underlying pattern, leading to poor generalization on unseen data. By introducing regularization, we constrain the magnitude of the model coefficients, forcing the model to be simpler and therefore more robust. This trade-off between bias and variance helps the model perform better on validation and test sets by ensuring that no single feature dominates the prediction process unnecessarily.

Can you explain how L2 regularization, also known as Ridge regression, works?

L2 regularization, or Ridge regression, adds a penalty equivalent to the square of the magnitude of the coefficients to the loss function. Mathematically, it adds lambda times the sum of the squared weights to the mean squared error. Because we are penalizing the square of the coefficients, the model is discouraged from assigning very high values to any individual feature weight. It essentially shrinks all coefficients toward zero but rarely makes them exactly zero. In code, you would use something like 'Ridge(alpha=1.0)' in a linear model pipeline. This is highly effective when you have multicollinearity among features because it distributes the weight more evenly across correlated variables.

What is L1 regularization, or Lasso regression, and how does it differ from Ridge?

L1 regularization, or Lasso regression, adds a penalty equivalent to the absolute value of the magnitude of the coefficients to the loss function. Unlike Ridge, which squares the weights, the L1 penalty term encourages sparsity in the model. This means that Lasso has the unique ability to push some feature weights all the way to exactly zero. Consequently, Lasso acts as a built-in feature selection method. In a practical machine learning workflow, you might use 'Lasso(alpha=0.1)' when you suspect that only a subset of your input features is actually relevant to the target variable, effectively eliminating noise from irrelevant predictors.

When should you choose L1 (Lasso) over L2 (Ridge) regularization?

You should choose L1 regularization when you have a high-dimensional dataset where you suspect that many features are irrelevant or redundant. Because L1 produces a sparse model by setting unimportant coefficients to zero, it simplifies the resulting model, making it more interpretable and easier to deploy. In contrast, you should choose L2 when you believe most of your features contribute to the outcome and you want to maintain them all while keeping their influence small. If your data suffers from high multicollinearity, L2 is generally preferred because it tends to shrink correlated coefficients together, whereas L1 might arbitrarily pick one and drop the others.

How does the hyperparameter lambda (or alpha) influence the model behavior?

The hyperparameter lambda controls the strength of the regularization penalty. When lambda is set to zero, no regularization is applied, and the model behaves like standard ordinary least squares, which is prone to overfitting if the data is noisy. As you increase lambda, you increase the penalty on large coefficients, effectively decreasing the model's complexity. A very high lambda will lead to underfitting because the model is forced to keep its weights so small that it loses the ability to capture the underlying signal of the data. Finding the optimal lambda is usually performed using cross-validation techniques, such as grid search, to identify the 'sweet spot' that minimizes test error.

What is the intuition behind the geometric shapes of L1 and L2 penalty constraints in weight space?

In weight space, the L2 constraint forms a hypersphere (a circle in 2D), whereas the L1 constraint forms a diamond shape with sharp corners on the axes. When we minimize the loss function subject to these constraints, we are looking for the point where the cost function's elliptical contours first touch the regularization constraint boundary. Because the L1 constraint has sharp corners located exactly on the axes, the elliptical contours are much more likely to hit the boundary at a point where one or more coefficients are zero. The L2 hypersphere lacks these corners, so the contact point is almost always in the middle of a curve, resulting in non-zero, small coefficients rather than sparse, zero-valued solutions.

All Machine Learning interview questions →

Check yourself

1. When comparing the weight decay behavior of L1 and L2, what happens to the coefficients as the penalty strength increases?

  • A.Both L1 and L2 drive coefficients to exactly zero.
  • B.L1 shrinks coefficients linearly toward zero, while L2 shrinks them exponentially.
  • C.L1 creates sparse models by setting many coefficients to zero, while L2 shrinks coefficients toward zero but rarely hits zero.
  • D.L2 forces coefficients to be positive, while L1 allows for negative values.
Show answer

C. L1 creates sparse models by setting many coefficients to zero, while L2 shrinks coefficients toward zero but rarely hits zero.
L1 uses the absolute value of coefficients, creating a 'diamond' constraint region that hits axes at zero, enabling sparsity. L2 uses the square, creating a 'circular' region that shrinks values asymptotically but doesn't reach zero. The other options misstate the fundamental geometry of the penalty terms.

2. Why is feature scaling essential before applying L2 (Ridge) regularization?

  • A.Unscaled features make the matrix inversion impossible.
  • B.The penalty term depends on the magnitude of the weights, which are biased by the scale of the corresponding input features.
  • C.L2 regularization only works on data that follows a normal distribution.
  • D.Scaling prevents the coefficients from becoming too large, which is the only goal of regularization.
Show answer

B. The penalty term depends on the magnitude of the weights, which are biased by the scale of the corresponding input features.
L2 penalizes the sum of squared weights. If one feature is measured in thousands and another in decimals, the model will shrink the weight of the former more aggressively regardless of predictive power. Scaling ensures all features contribute equally to the penalty. Other options are technically incorrect regarding matrix inversion or the scope of regularization.

3. In a dataset with high multicollinearity among independent variables, how does Ridge regression behave compared to Ordinary Least Squares (OLS)?

  • A.Ridge regression will perform worse because it introduces bias.
  • B.Ridge regression remains stable by penalizing the squared magnitudes of coefficients, effectively distributing the weight across correlated features.
  • C.Ridge regression will set all correlated coefficients to zero to simplify the model.
  • D.Ridge regression is computationally faster than OLS for highly correlated datasets.
Show answer

B. Ridge regression remains stable by penalizing the squared magnitudes of coefficients, effectively distributing the weight across correlated features.
OLS becomes unstable with multicollinearity because small changes in data cause large swings in coefficients. Ridge regularization adds a small bias to reduce variance, distributing weights among correlated features rather than causing explosive coefficient values. Ridge is not faster, nor does it inherently drop correlated features.

4. What is the primary motivation for using the Elastic Net penalty over pure Lasso (L1)?

  • A.Elastic Net is always faster to calculate than Lasso.
  • B.Lasso fails to produce any non-zero coefficients.
  • C.When features are highly correlated, Lasso tends to pick one randomly, whereas Elastic Net retains groups of correlated features.
  • D.Elastic Net allows the model to predict non-linear relationships.
Show answer

C. When features are highly correlated, Lasso tends to pick one randomly, whereas Elastic Net retains groups of correlated features.
Lasso's selection can be unstable with correlated features. Elastic Net adds an L2 component that 'groups' correlated features, keeping them in the model together. The other options are incorrect: Elastic Net is usually slower, Lasso does produce non-zero coefficients, and both remain linear models.

5. Suppose you are tuning the alpha parameter for Ridge regression. As alpha approaches infinity, what happens to the model?

  • A.The model becomes identical to Ordinary Least Squares.
  • B.The coefficients move toward zero, causing the model to underfit.
  • C.The model becomes highly complex to capture every detail of the noise.
  • D.The intercept term also forced to be zero.
Show answer

B. The coefficients move toward zero, causing the model to underfit.
As alpha increases, the penalty becomes so dominant that the cost function is minimized only when all coefficients (except potentially the intercept) are zero. This leads to high bias and underfitting. OLS occurs at alpha=0, and increasing complexity is the opposite effect of high regularization.

Take the full Machine Learning quiz →

← PreviousLearning Rate and SchedulersNext →Early Stopping

Machine Learning

35 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app