Welcome to the final lesson of our course on the essential mathematics for machine learning. In the last session, we explored how Ridge regression not only helps prevent overfitting by shrinking model coefficients but also serves as a crucial numerical stabilizer. By adding a term to the matrix , we effectively "lift" its eigenvalues, improving its condition number and making the loss landscape more uniform and easier to optimize.
Today, we shift our focus to another powerful regularization technique: Lasso regression. You will learn how changing the penalty from the squared L2 norm to the absolute L1 norm leads to a profoundly different and highly desirable behavior: sparsity. By the end of this lesson, you will be able to explain geometrically why Lasso tends to set many model coefficients to exactly zero, effectively performing automatic feature selection, and you'll be able to contrast this behavior with that of Ridge regression. This final lesson ties together concepts of optimization, geometry, and model interpretability, providing a capstone to your journey through linear algebra and calculus for machine learning.
From L2 to L1: A Subtle Change with Big Consequences
Let's recall the objective function for Ridge regression, which adds an L2 penalty to the Residual Sum of Squares (RSS):
Lasso, which stands for "Least Absolute Shrinkage and Selection Operator," makes a simple but critical modification. It replaces the L2 norm with the L1 norm:
This change from squaring the coefficients to taking their absolute value is the key to everything that follows. While Ridge regression penalizes large coefficients heavily, Lasso applies a constant penalty to coefficients regardless of their magnitude, which has a fascinating consequence as they approach zero.
Lasso and Sparsity in Statistics
This document introduces the core idea behind Lasso regression.
Please read the introductory paragraphs, starting from the beginning. Pay close attention to the mathematical formulation of the Lasso objective and the statement that it delivers a "sparse solution." This sparsity is the central theme of today's lesson.
Unlike Ridge regression, the Lasso objective function doesn't have a convenient closed-form solution because the absolute value function is not differentiable at . This means we can't simply take the derivative, set it to zero, and solve. Instead, Lasso solutions are found using iterative optimization algorithms. However, the lack of a simple solution is a small price to pay for its most powerful feature: the ability to perform variable selection.
The Geometric View: A Diamond in the Rough
The most intuitive way to understand why Lasso produces sparse solutions is by visualizing the optimization problem. As with Ridge, we can frame Lasso as a constrained optimization problem: we want to find the model coefficients that minimize the RSS, subject to a constraint on the size of the coefficients.
- Ridge (L2) Constraint:
- Lasso (L1) Constraint:
Let's consider a simple 2D case with two coefficients, and . The Ridge constraint defines a circular region. The Lasso constraint defines a diamond-shaped region (a square rotated by 45 degrees).
The solution to the regression problem is the point where the elliptical contours of the RSS function first touch the boundary of this constraint region.

Look closely at the image above. The circular L2 constraint for Ridge is smooth. The point of contact can be anywhere on its perimeter. It is highly improbable that this point will lie exactly on an axis (where a coefficient would be zero).
The diamond-shaped L1 constraint for Lasso, however, has sharp corners that lie on the axes. If the RSS ellipse expands and touches one of these corners, the corresponding coefficient becomes exactly zero. In higher dimensions, this diamond becomes a hyper-diamond (an octahedron) with many corners and edges, all of which correspond to settings where some coefficients are zero. This geometry makes zero-valued coefficients not just possible, but probable.
The following video provides a fantastic animated explanation of this geometric intuition.
Why L1 Regularization Produces Sparse Weights (Geometric Intuition)
This video from DataMListic clearly visualizes why the L1 penalty leads to sparsity.
Watch the first part from the beginning, which explains how the RSS contour circle expands to meet the diamond-shaped L1 constraint. Notice how the corners, which represent sparse solutions, are natural contact points. Then, watch the comparison with L2 regularization from this segment. The key takeaway is that the L2 circle's lack of corners makes zero-valued coefficients unlikely.
The Loss Function Landscape: A Kink at Zero
Another way to understand Lasso's behavior is to visualize the shape of the penalty function itself.
- The L2 penalty, , is a smooth parabola.
- The L1 penalty, , is a sharp 'V' shape.
When we add these penalties to the RSS loss (which is also a parabola), the shape of the total loss function changes.
- Ridge: Adding two parabolas results in another, shifted parabola. Its minimum will be closer to zero than the original RSS minimum, but it will only be at zero if the original minimum was already there.
- Lasso: Adding a parabola and a 'V' shape creates a new function that has a sharp kink at zero. If the RSS minimum is close enough to zero, this kink can "pull" the total minimum to be exactly at zero.
The StatQuest channel provides one of the best visual explanations of this phenomenon.
Ridge vs Lasso Regression, Visualized!!!
Josh Starmer visualizes Ridge and Lasso regression by plotting their 1D loss functions.
First, quickly review the setup from the beginning, which shows the standard parabolic RSS loss. Next, watch how the L2 (Ridge) penalty shifts the parabola towards zero from this section. Notice that the minimum gets closer to zero but never quite reaches it. Now, see the crucial difference with the L1 (Lasso) penalty from this part. Observe how the absolute value penalty creates a "kink" at zero, and as \lambda increases, this kink can become the lowest point of the entire function. Finally, watch the summary comparison from the end for a great recap of the visual difference.
This "kink" is the 1D equivalent of the "corners" in the geometric view. Both are non-differentiable points that create regions of attraction for the optimal solution where coefficients are exactly zero.

Sparsity in Practice: Automatic Feature Selection
The tendency of Lasso to drive coefficients to zero is called sparsity. This is not just a mathematical curiosity; it's an incredibly useful property for building machine learning models, especially in fields like computational biology where you might have thousands of potential features (e.g., genes) and only a few are truly predictive of an outcome.
- Interpretability: A sparse model is easier to interpret. If a model for cancer prediction uses 5,000 genes but Lasso sets the coefficients for 4,990 of them to zero, you can focus your analysis on the 10 genes the model deemed important.
- Feature Selection: Lasso performs an automatic and continuous form of feature selection. As you increase the regularization parameter , more and more coefficients are "zeroed out," effectively removing their corresponding features from the model.
- The "Bet on Sparsity" Principle: Lasso works best under the assumption that the true underlying signal is sparse—that is, only a few inputs truly affect the output. If this assumption holds, Lasso is highly efficient at finding that sparse signal.
Lasso and Sparsity in Statistics
This section discusses the practical implications of sparsity.
Read the section beginning with "The original motivation for the lasso". This connects the mathematical property of sparsity to its value in statistics and machine learning, particularly in high-dimensional settings like those found in genomics (p >> N).
Ridge vs. Lasso: A Summary
Both Ridge and Lasso are powerful techniques for improving upon standard linear regression, but they serve different purposes.
| Criteria | Lasso Regression | Ridge Regression |
|---|---|---|
| Penalty Term | L1 Norm: $ \lambda \sum | \beta_j |
| Constraint Shape | Diamond (or hyper-diamond) | Circle (or hypersphere) |
| Sparsity | Produces sparse models (some ) | Produces non-sparse models ( but rarely ) |
| Key Benefit | Automatic feature selection | Handles multicollinearity well, numerical stability |
| Solution | No closed-form, requires iterative solver | Has a closed-form solution |
| Best Use Case | When you believe many features are irrelevant | When you expect most features to be relevant, but perhaps correlated |
For a deeper dive into these comparisons, including a brief look at the Bayesian interpretation, the following resource is excellent.
This document provides a comprehensive comparison of the two methods.
First, review the geometrical comparison, which reinforces the diamond-vs-circle intuition we've discussed. Next, read the section on the Bayesian interpretation. It explains that Ridge corresponds to a Gaussian prior on the weights, while Lasso corresponds to a Laplace prior, whose sharp peak at zero provides another way to understand sparsity. This connects to the probabilistic concepts in your other course. Finally, look over the summary table and discussion, which neatly encapsulates the key differences.
Conclusion: A Well-Stocked Mathematical Toolkit
In this final lesson, you have seen how a simple change in the penalty norm—from L2 to L1—transforms a regularization method from a tool for coefficient shrinkage and stabilization (Ridge) into a tool for automatic feature selection (Lasso).
Key Takeaways:
- Lasso's Mechanism: Lasso regression adds an L1 penalty () to the loss function.
- Geometric Intuition: This corresponds to a diamond-shaped constraint region. The sharp corners of this diamond lie on the axes, making it probable that the optimal solution will have some coefficients that are exactly zero.
- Loss Function Intuition: The L1 penalty creates a "kink" at zero in the loss function, which can "pull" the minimum to a point where a coefficient is exactly zero.
- Sparsity and Feature Selection: This behavior produces sparse models, which are more interpretable and useful in high-dimensional settings where many features may be irrelevant.
- Ridge vs. Lasso: Ridge is ideal for handling multicollinearity when most features are useful, while Lasso is ideal for feature selection when you suspect many features are redundant or useless.
Congratulations on completing this course. You began with the fundamentals of vectors and matrices and have progressed through eigenvalues, SVD, PCA, multivariable gradients, optimization, and finally, to sophisticated regularization techniques that are at the heart of modern machine learning. You have built a strong foundation of geometric and conceptual intuition that will allow you to not just use machine learning models, but to understand how they work, why they fail, and how to interpret their results. This mathematical literacy is the key to moving beyond "black box" thinking and is an invaluable asset for your future work in computational biology and beyond.