Lasso Regularization (L1 Regularization) › Example
Machine Learning from ScratchAll chapters Lasso Regularization (L1 Regularization) › ExampleOne section · Sections in this chapter6 / 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
Let's work through a simple example to understand how LASSO regularization works. Suppose we have a dataset with 3 features and we want to predict house prices. Our model has the following coefficients:
- (feature 1: square footage)
- (feature 2: distance from city center)
- (feature 3: number of bathrooms)
Step 1: Calculate the L1 norm
The L1 norm is the sum of absolute values of the coefficients:
where:
-: index over the three features
- : absolute value of coefficient
- : total L1 penalty (sum of absolute coefficient values)
Step 2: Apply different regularization strengths
Note that in below examples, we are only considering the regularization term. We are not considering the sum of squared errors term to keep the examples simple enough to follow.
Let's see how different values of affect the model. Start with understanding the optimization process. LASSO finds the optimal coefficients by solving an optimization problem that balances two competing objectives:
- Fit the data well: Minimize the Sum of Squared Errors (SSE)
- Keep the model simple: Minimize the L1 penalty term
The larger is, the more the model prioritizes simplicity over fitting the training data perfectly. This trade-off is what prevents overfitting.
Think of it this way: LASSO is like having a "budget" for how large your coefficients can be. The penalty term acts like a "cost" that increases as coefficients get larger.
- Small : Low cost for large coefficients → model can afford to keep coefficients large to fit data well
- Large : High cost for large coefficients → model needs to make coefficients smaller to stay within "budget"
The optimization algorithm finds the point where reducing a coefficient further would hurt the model's ability to predict more than it helps reduce the penalty cost.
Case 1: (No regularization)
- Penalty term: , so when , the objective function becomes: The penalty term disappears completely, so LASSO becomes identical to ordinary least squares regression. Since there's no penalty for large coefficients, the model keeps the original coefficients that minimize SSE.
- These are the final coefficients: , , (unchanged)
The coefficient values shown in these examples are illustrative and simplified for educational purposes. In practice, the exact values depend on the specific data and the optimization algorithm used. The key insight is understanding how different values affect the balance between fitting the data and keeping coefficients small.
Case 2: (Moderate regularization)
- Penalty term: , so LASSO solves:
The penalty affects the coefficients as follows (conceptually):
- (square footage): Original magnitude = 0.8, penalty cost = , so the model reduces it to 0.6 (still useful, but cheaper)
- (distance): Original magnitude = 0.3, penalty cost = , so the model reduces it from -0.3 to -0.2 (smaller penalty, still captures the negative relationship)
- (bathrooms): Original magnitude = 0.1, penalty cost = , so the model reduces it from 0.1 to 0.05 (smallest coefficient, reduced proportionally) This happens because the penalty creates a "cost-benefit analysis" for each coefficient. Important features get reduced less than unimportant ones. (Exact values depend on the data; numbers here are illustrative.)
Case 3: (Strong regularization)
- Penalty term: , so LASSO solves:
The strong penalty affects each coefficient as follows (conceptually):
- (square footage): Original magnitude = 0.8, penalty cost = . The model asks: "Is keeping worth the penalty cost of 1.6?" Since square footage strongly affects house prices, the model keeps it but reduces it significantly: . The new penalty cost = (much cheaper!)
- (distance): Original magnitude = 0.3, penalty cost = . The model asks: "Is keeping worth the penalty cost of 0.6?" Distance is less important than square footage, so the model sets it to exactly zero. The new penalty cost = (free!)
- (bathrooms): Original magnitude = 0.1, penalty cost = . The model asks: "Is keeping worth the penalty cost of 0.2?" Bathrooms have minimal impact, so the model sets it to exactly zero. The new penalty cost = (free!)
(Exact values depend on the data; numbers here are illustrative.)
Step 3: The Mathematical Optimization Process
Let's trace through what happens mathematically. For each case, LASSO solves:
where:
-: coefficients for the three features
- : sum of squared errors as a function of the coefficients
- : regularization parameter
- : absolute value of coefficient
For :
The algorithm starts with the OLS coefficients and iteratively adjusts them
- It finds that reducing from 0.8 to 0.6 reduces the penalty by while only slightly increasing SSE
- It finds that reducing from -0.3 to -0.2 reduces the penalty by while only slightly increasing SSE
- It finds that reducing from 0.1 to 0.05 reduces the penalty by while only slightly increasing SSE
For :
The algorithm finds that the penalty cost for keeping and is too high
- Setting eliminates a penalty cost of
- Setting eliminates a penalty cost of
- The total penalty reduction (0.8) outweighs the increase in SSE from removing these features
This is the essence of LASSO: it performs a cost-benefit analysis for each coefficient, where the "cost" is the penalty term and the "benefit" is the reduction in SSE.
| # | Penalty | What Happens | Result | |
|---|---|---|---|---|
| 1 | 0.0 | None | No penalty, pure OLS | All coefficients unchanged |
| 2 | 0.5 | Moderate | Balanced penalty | Coefficients reduced but all kept |
| 3 | 2.0 | Strong | Heavy penalty | Some coefficients become exactly zero |