- Published on
Regularization in ML | Lasso | Part 2/3
- Authors
- Name
- Vishal Mandrai
In this second post, we'll explore Lasso Regularization in detail. How it works? Optimization technique used for solution finding? Why it shrinks coefficients to zero and induce sparsity? Geometric Interpretations of Lasso Regularization. Sound's interesting? Let's jump in.
Lasso Regression - L1 Regularization
LASSO stands for “Least Absolute Shrinkage and Selection Operator”. It is a type of regularization technique used in linear regression and other models to prevent overfitting and perform feature selection. In Lasso Regression, the cost function of linear Regression is modified by adding a Penalty Term. This penalty term is nothing but sum of absolute values of the model coefficients.
Lasso Loss Function:
where:
- → Mean Squared Error (MSE)
- → L1 Penalty (sum of absolute values)
- → Strength of regularization
NOTE: The intercept is not included in the Penalty Term.
Key Features
| Feature | Lasso |
|---|---|
| Type of penalty | L1 (absolute value) |
| Effect on coefficients | Shrinks; Some to exactly 0 |
| Feature selection | Yes — selects important features |
| Handles multicollinearity | Partially (by picking one) |
| Convexity | Yes (but not differentiable at 0) |
Why Use Lasso Variant over Vanilla Linear Regression?
- Prevents Overfitting: prevents model to fit random noise in data and cause exploding coefficients.
- Automatic Feature Selection: features with smaller coefficients are already contributing less in predictions, they are less important. Lasso makes their coefficients exactly zero. Hence they are removed from the model.
- Slash Model Complexity: Useful when data have many input features but only a few are useful.
Mathematical Formulation & Solution Using Coordinate Descent
Lasso Regression modifies the standard linear regression objective by adding a ‘Penalty Term’ which is sum of the absolute values of the coefficients. We know, Lasso Loss Function:
Given:
- is the input matrix. Total input features.
- is the number of records in training set.
- is the target vector.
- is the vector of coefficients (excluding intercept).
- is hyperparameter; controlling the amount of shrinkage.
One big issue in finding solution for the above cost function – LOSS FUNCTION IS NOT DIFFERENTIABLE AT 0. Hence Gradient Descent Optimization Algorithm can't work.
Gradient Descent (GD) requires a single, definitive slope at every point to update the weights. Lasso Regression prevents this because of the L1 penalty term breaks the mathematical rule of differentiation. Mathematical rule that, for a function to have a derivative at a point, the slope approaching from the left must equal the slope approaching from the right. The derivative at is undefined.
When Gradient Descent runs its weights update step. If a weight hits exactly , the algorithm requests the value of . The computer receives an undefined mathematical error (NaN or division failure), causing standard Gradient Descent to break down.
So, the objective is convex but not differentiable. Due to this reason, we don’t have a Closed-Form Solution in Lasso Regression. Here we’ll utilise a new optimization technique – “Coordinate Descent”. Let's quickly see what it is.
Coordinate Descent Algorithm
Coordinate Descent is an Optimization Algorithm that minimizes a multivariate objective function by iteratively optimizing it along one coordinate (variable) at a time while keeping all other coordinates fixed. It is particularly useful for high-dimensional problems where gradient-based methods may be computationally expensive.
Coordinate Descent (CD) updates only one variable per iteration, unlike GD which updates all the variables in an iteration. It can also work with non-differentiable functions (depending on variant). Highly efficient for large-scale problems. Convergence guaranteed under certain conditions like convexity and smoothness.
NOTE: In Lasso Regression implementation of Scikit-learn, Coordinate Descent Algorithm is used.
Types of Coordinate Descent:
- Cyclic Coordinate Descent: Updates variables in a fixed order (e.g., w1, w2, …, wp).
- Randomized Coordinate Descent: Picks a variable randomly in each iteration.
- Greedy Coordinate Descent: Search and selects the variable that leads to the largest descent.
Let's apply Coordinate Descent in Lasso Regression to find the optimal solution.
Lasso Regression Solution - via CD
The coefficient update step for coefficient in an iteration of Coordinate Descent can be given as:
where:
- – Current Iteration; – Previous Iteration
- = values of coefficients after iterations
- Except for all other coefficients are fixed in the above equation
- = updated value of coefficient , after iterations
After this iterations, updated coefficients are:
Because, was the only coefficients which was updated in iteration.
Exact Update Step can be given as:
In an optimization iteration, for coefficient , Lasso update is:
where:
- is the model's prediction without the contribution of .
- Soft Thresholding means:
How do we arrive at this weird looking Soft Thresholding Operation? And how this operation works? Let's see this next and derive these results.
Derivation
The Loss Function for Lasso Regression is:
Now, differentiate w.r.t keeping other coefficients fixed. Then, find the value of for which the derivative is zero.
Equating above equation with to get the value of for which it is minimum:
Taking out the term from the summation, where calculating residuals, excluding the effect of input feature and coefficient on the prediction:
Now, Assuming input feature is standardized. Hence So, the final update step for is:
NOTE: Sign of is the same as the sign of (when ). How? We'll discuss this.
The above equation is equivalent to “Soft Thresholding Operation”. So,
Let's take a quick detour to understand "Soft Thresholding" - the operation that appears naturally in Lasso Regression due to L1-penalty and shrinks coefficients (some to zero) and induce sparsity.
Soft Thresholding Operation
Soft Thresholding (also called shrinkage) is a mathematical operation used in signal processing and optimization, particularly in sparse regressions. It shrinks values towards zero, hence effectively inducing sparsity.
Think of it like a special function, given a scalar and a threshold , the soft-thresholding operator is defined as:
Interpretation:
- If means is shrunk towards zero.
- If means is thresholded to zero.
Soft Thresholding Operation appears naturally in Coordinate Descent based optimization for lasso regression, and its all because of L1-penalty term.
If we look closely, in Lasso Regression, our sole job is to minimize the loss function and find the optimal set of coefficients. Its L1-penalty term in loss function that leads to application of soft thresholding operation on ordinary least-squares regression solution (a solution without any regularization) of the problem. This can be denoted as:
Here, is the roughly equal to ordinary least-squares solution without regularization (roughly because we are still missing the contribution of in residual calculation).
So, you see the hidden master that shrinks coefficients and causes sparsity? Yes, its Soft Thresholding Operation that comes naturally due to L1-penalty term in Coordinate Descent based optimization, because the loss function is not differentiable at 0 and we cannot use Gradient Descent based optimization.
Back to Derivation
One question still remains. How did we go from below equation to Soft Thresholding Operation? Let’s see.
We know that, when we apply Coordinate Descent, we make single weight update in an iteration keeping all other coefficients fixed. To solve the above equation we know the .
Now here the problem arise. Coefficient is a variable and is the sign of optimised or updated , but we don’t know what would be the future sign of , will it be or or no sign i.e., .
What if I tell you that their is a hack to find it via "" i.e., .
Let's see for different possible values of how does the optimal solution looks:
I. If updated i.e., be :
- Sub-gradient
- Optimality condition:
Requirements:
- For updated , otherwise and would contradict the assumption of .
- Means updated , only and only when, . Since , must be positive for .
- This way aligns with .
II. If updated i.e., be :
- Sub-gradient
- Optimality condition:
Requirements:
- For updated , otherwise and would contradict the assumption of .
- Means updated , only and only when, . Since , must be negative for .
- This way aligns with .
III. If updated :
- Sub-gradient
- Optimality condition:
Requirements:
- For updated , otherwise and would contradict the assumption of .
Hence the equation from derivation can be re-written as:
Look closely this is nothing but Soft Thresholding Operation on and . Hence:
So, we just completed a very crucial derivation and also uncovered what actually causes shrinkage of coefficients.
For extra curious beings, I have written an separate article, where I have geometrically explained how aligns with . Do check out!
Geometric Intuition of how sign(𝑤ⱼ) aligns with sign(z) in Lasso Regression
What Causes Shrinking of Weights In Lasso?
Although via derivation this question can be answered best. I you request for a quick read for in-depth understanding. In this section let's summarise our understanding.
The main reason behind Lasso Regression causing shrinkage of coefficients and reducing some coefficients to absolute zero is "Soft Thresholding Operation" that appears naturally in its solution, due to L1-penalty term in the Lasso Cost Function, for which optimal solution is found via Coordinate Descent based optimization.
Soft Thresholding Operation causes shrinkage and results in the coefficients being reduced—by a factor of towards zero, promoting sparsity in the model.
Effect of Soft Thresholding Operation be:
- Large weights are pulled toward zero by .
- Small weights are made absolute zero.
NOTE: Small coefficient of a input feature means the feature has low predictive power and small effect on the target variable. We can get rid of them.
Intuition: The L1 penalty "steals" units of magnitude from each weight. If the weight is too small i.e., , it is set to . Weak features are eliminated. Strong features are retained but penalized.
This also explains how Lasso helps in "Feature Selection".
Geometric Interpretation
To geometrically interpret the Lasso Regression, we need to view it as a constrained optimization problem. In Lasso, we aim to minimize: Least-squares loss + L1 Penalty
The least-squares loss forms a convex, smooth quadratic bowl in the parameter space. Whereas, the L1-penalty corresponds to a diamond-shaped constraint region (non-smooth at the axes due to the absolute value function i.e., ). Hence, we need to optimize the least-squares loss while satisfying the constraint imposed by the L1 penalty.
Geometrically, this means finding the point on the L1 constraint boundary where the quadratic loss is minimum.
Check the below graph. Showing contours of leas-square loss and penalty constraint boundaries of Ridge and Lasso Regression in a 2-D plot:

Geometric Interpretations of i. Ridge (left) and ii. Lasso Regression (right)
The least-square loss part of the objective function makes elliptical contours in weight space. Example for a lasso regression with 2 feature coefficients to optimize, a 2-D ellipse centred at the least-squares solution. Coefficient values at the centre of these elliptical contours cause overfitting. Each point on the contour yields same residual error.
The L1-penalty constraint region (when viewed as a constrained optimization problem) forms a diamond (or rhombus) in 2-D weight space.
Geometrically, the point of optimal solution is the point where the elliptical contours of least-square loss just touch the diamond-shaped constraint region of L1-penalty.
For Lasso Regression, these point of contacts are often at the corner of that diamond and not sides. These corners represent points where one or more coefficients are exactly zero. This is why Lasso promotes sparsity.
It is geometrically more likely that the minima occurs at or near the corners.
Contrast with Ridge Regression
Ridge Regression uses an L2-penalty constraint, which forms a circular constraint (or spherical boundary in higher dimensions).
The intersection of circles with ellipses is less likely to hit axes. So in ridge regression, coefficients typically shrink but do not set them to zero.
Here, we are done with Lasso Regression. Next we'll jump into "Elastic-Net Regression" - which offers the best of both worlds.