Lasso Regularization (L1 Regularization) › Matrix Notation
Machine Learning from ScratchAll chapters Lasso Regularization (L1 Regularization) › Matrix NotationOne section · Sections in this chapter4 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
We can rewrite the LASSO optimization problem in matrix notation, which provides a more compact and mathematically elegant representation. This notation is particularly useful for understanding the relationship between LASSO and other optimization problems, and it's the form commonly used in machine learning libraries like scikit-learn.
From Summation to Matrix Form
Let's start with our familiar summation form and transform it step by step:
Step 1: Define the matrices and vectors
First, let's define our data in matrix form:
- : An vector containing all target values:
- : An matrix containing all features (including a column of ones for the intercept):
- : A vector containing all coefficients (including intercept):
Step 2: Express predictions in matrix form
The predicted values for all observations can be written as:
This single matrix multiplication computes all predictions simultaneously. Row of multiplied by gives:
This is exactly the prediction for observation :
Step 3: Express residuals in matrix form
The residuals (errors) for all observations are:
Step 4: Express the sum of squared errors in matrix form
The sum of squared errors becomes:
Let's verify this step by step:
Since , we have , thus:
Step 5: Express the L1 penalty in matrix form
The L1 penalty term can be written using the L1 norm:
where is the L1 norm of the coefficient vector. This means that the L1 penalty is the sum of the absolute values of the coefficients, or otherwise known as the "Manhattan distance" between the coefficients and the origin. It is the same as summing up all the features:
Final matrix form:
What we have now is a compact way of writing the LASSO optimization problem using matrix notation.
The Scikit-learn Formulation
Scikit-learn uses a slightly different but equivalent formulation that includes a normalization factor:
where:
- : regularization parameter in scikit-learn, related to traditional by
- : number of observations in the dataset
- : squared L2 (Euclidean) norm of residuals, equivalent to (the SSE)
- : L1 (Manhattan) norm of coefficients, equal to (sum of absolute values, intercept not penalized)
The expression uses the L2 norm (also called the Euclidean norm):
where:
- : L2 norm of the residual vector (Euclidean distance)
- : actual target value for observation
- : predicted value for observation , equal to
- : number of observations
When we square the L2 norm, we get:
This is exactly our sum of squared errors (SSE)! The L2 norm squared gives us the same quantity as our matrix form . The L2 or Euclidean norm is simply the square root of the sum of the squares of the elements of the vector.
We use the L2 norm notation because it is mathematically equivalent to the matrix form , but more concise and widely recognized. The L2 norm is standard in optimization and statistics, making formulas easier to read and interpret. This notation also highlights the connection to Euclidean distance, which helps build intuition about how the loss function measures the overall error.
We now got the left side of the equation, let's look at the right side. The expression is the L1 norm of the coefficient vector:
where:
- : L1 norm (Manhattan distance) of the coefficient vector
- : absolute value of the -th coefficient
- : number of features (excluding intercept)
This is exactly our L1 penalty term! The L1 norm is simply the sum of the absolute values of the elements of the vector.
Note: The intercept is typically not included in the L1 penalty, so the sum runs from to , not from to .
We know that the L1 norm offers several important advantages: it encourages sparsity by driving some coefficients exactly to zero, which enables automatic feature selection. Further, it has a clear geometric interpretation: it measures the "Manhattan distance" (the sum of absolute values) from the origin in coefficient space, and is a convex function, which ensures that the optimization problem remains tractable and that efficient algorithms can find a global minimum.
The Normalization Factor:
Let's take a closer look at the normalization factor that appears in front of the L2 norm in the scikit-learn formulation. In scikit-learn, the regularization strength is controlled by the parameter alpha (), which is related to the traditional parameter by .
This means that the scikit-learn objective function is normalized by both the number of samples and a factor of 2, making the loss function consistent with the mean squared error and simplifying gradient calculations.
This formulation is sometimes referred to as the "normalized" or "scaled" LASSO objective, and is the standard approach used in most modern machine learning libraries.
The factor serves several important purposes:
-
Sample size normalization: Dividing by makes the loss function independent of sample size. Without this, larger datasets would have larger loss values simply because they have more observations.
-
Gradient scaling: The factor simplifies the gradient calculation. Let's take the derivative of with respect to . To see how we arrive at this gradient, let's rewrite the squared L2 norm and take the derivative step by step:
Let's see how we can rewrite the term in a more manageable form. The term is a quadratic form that represents the sum of squared errors in matrix notation. To make it easier to take derivatives and understand the structure, we can rewrite it using the distributive property of matrix multiplication:
Notice that is a scalar and is equal to its transpose , so the two middle terms combine to . The last term, , can be rewritten as . Putting it all together, we get:
Taking the derivative with respect to :
- The derivative of with respect to is 0 (since it does not depend on )
- The derivative of with respect to is
- The derivative of with respect to is
So, the derivative is:
where:
- : transpose of the feature matrix
- : target vector
- : coefficient vector
Including the factor:
Or, equivalently (by factoring out the negative sign):
where:
- : normalization factor (average over all observations)
- : gradient of the squared error term with respect to
The gradient points in the direction of steepest ascent of the loss function. In gradient descent for minimization, we move in the opposite direction by subtracting this gradient from the current . Without the factor, we would have an extra factor of 2 in the gradient.
-
Consistency with statistical literature: This normalization makes the objective function consistent with the mean squared error (MSE), since is exactly the MSE.
The Regularization Parameter:
In scikit-learn, the regularization parameter is called alpha () instead of lambda (). The relationship between the two formulations is:
where:
- : scikit-learn regularization parameter
- : traditional LASSO regularization parameter
- : number of observations in the dataset
This means:
- Small : Less regularization, coefficients closer to OLS solution
- Large : More regularization, more coefficients driven to zero
Complete Mathematical Breakdown
Let's put it all together with a concrete example. Suppose we have:
- observations
- features (plus intercept)
The scikit-learn objective function becomes:
where:
- : normalization factor ( where )
- : regularization parameter
- : squared L2 norm of residuals
- : L1 norm of coefficients
Expanding this:
where:
- : index over observations
- : index over features
- : actual target value for observation
- : predicted value for observation
- : coefficient for feature
This is equivalent to:
where:
- : intercept term (not penalized)
- : value of feature for observation
- : absolute value of coefficient
Why This Formulation Matters
Matrix notation has several important practical benefits. First, matrix operations are highly optimized in modern numerical libraries. Second, expressing the objective in terms of norms directly connects LASSO to the broader field of convex optimization. Third, this standardized form is what most machine learning libraries, such as scikit-learn, use in their implementations. Finally, this framework is flexible: it extends naturally to other regularization techniques like Ridge regression and Elastic Net, which will be discussed in later sections.
By understanding the matrix formulation, you will be better equipped to interpret results from scikit-learn, select appropriate values for the alpha parameter, recognize the connections between different regularization methods, and even implement custom optimization algorithms if your application requires it.
Visualizing the Regularization Process
After all of this math, let's take a look at a visualization that demonstrates how L1 regularization works by showing how coefficients change as the regularization parameter increases. This is called a "coefficient path" plot.
Recall that scikit-learn's Lasso solves the objective function , where . This objective function defines what the algorithm is optimizing, and the choice of directly controls the strength of regularization. In the plot below, we vary to show its effect on the coefficients. The underlying implementation typically uses an algorithm such as the Iterative Shrinkage-Thresholding Algorithm (ISTA) or coordinate descent to efficiently solve this objective, but the objective function itself is what determines the solution.

-
Left (small ): All coefficients are close to their true values. There's minimal regularization.
-
Middle: As increases, coefficients start shrinking toward zero. Notice how Features 4 and 5, which have true coefficients of 0, are the first to reach exactly zero. This demonstrates automatic feature selection.
-
Right (large ): All coefficients are driven toward zero as the penalty dominates.
Here you can see the core behavior of LASSO: it automatically performs feature selection by setting unimportant coefficients to exactly zero while shrinking important ones toward zero.