Hello! Welcome to our next lesson on building robust machine learning models.
In our previous session, we explored the bias-variance tradeoff, the fundamental tension between models that are too simple (high bias) and those that are too complex (high variance). We saw that overly complex models, while showing impressive performance on training data, often fail to generalize to new data—a problem known as overfitting. One of the key strategies we identified to combat this was regularization.
Today, we will put that strategy into practice. Our goal for this lesson is to apply Ridge (L2) and Lasso (L1) regularization to mitigate overfitting and perform feature selection. These are two of the most fundamental and widely used techniques for creating simpler, more robust linear models that perform better in the real world. This is a crucial step in your goal to not just build models, but to justify why your chosen model is the right one.
The Problem with Large Coefficients
Let's start by revisiting the core issue with overfitting. As we saw with polynomial regression in the last lesson, when a model becomes too complex, its coefficients can become very large. The model starts fitting the noise in the training data, leading to a "wiggly" and unstable function. A large coefficient means that a small change in an input feature can cause a huge change in the output, making the model highly sensitive and prone to high variance.
Regularization tackles this problem head-on by adding a penalty to the model's loss function. Instead of just trying to minimize the error (like the Sum of Squared Errors, or SSE), the model now has to minimize:
This penalty term is designed to punish large coefficients, forcing the model to find a balance between fitting the data well and keeping its coefficients small.
Fighting Overfitting With L1 or L2 Regularization
To start, let's get a concise overview of regularization from the article 'Fighting Overfitting With L1 or L2 Regularization' on Neptune.ai.
Please read the first section, 'What is regularization and why do we need it?'. This will provide a clear, high-level summary of the concept we are about to explore.
Now, let's explore the two most common ways this penalty is applied: Ridge and Lasso regression.
Ridge Regression (L2 Regularization): The Shrinker
Ridge regression is our first tool for controlling model complexity. It adds a penalty proportional to the sum of the squares of the coefficients. This is also known as the L2 norm.
The objective function for Ridge regression becomes:
Where:
- is the standard Sum of Squared Errors (SSE).
- is the coefficient for the j-th feature.
- (often denoted as ) is the regularization strength. It's a hyperparameter we control.
- If , the penalty is zero, and Ridge regression is identical to ordinary linear regression.
- As increases, the penalty for large coefficients becomes more severe, forcing the coefficients to shrink closer to zero.
- Note: The intercept term () is typically not included in the penalty.
This penalty forces the model to use smaller coefficients. While it shrinks them towards zero, it will very rarely set any of them exactly to zero.
To build a strong intuition for how Ridge regression works, the following video is an excellent guide.
Regularization Part 1: Ridge (L2) Regression
The video 'Regularization Part 1: Ridge (L2) Regression' from StatQuest with Josh Starmer provides a fantastic visual and intuitive explanation.
Please watch from the beginning to 07:27 and then from 08:40 to 10:26. Finally, watch the short segment from 13:59 to 15:20. Focus on: The core idea of trading some bias for a drop in variance (03:32). How the penalty term is added to the least squares objective (04:15). The role of the hyperparameter lambda (alpha) and how it's chosen using cross-validation (08:40). The important detail that the intercept is not penalized (13:59).
Lasso Regression (L1 Regularization): The Feature Selector
Lasso regression works in a similar way to Ridge, but with one crucial difference in its penalty. It adds a penalty proportional to the sum of the absolute values of the coefficients. This is known as the L1 norm.
The objective function for Lasso regression is:
This seemingly small change from squaring () to taking the absolute value () has a profound effect. While Ridge shrinks coefficients towards zero, Lasso can shrink them all the way to zero.
This means Lasso can perform automatic feature selection. If a feature's coefficient becomes zero, that feature is effectively removed from the model. This is incredibly useful when you have a dataset with many features, some of which may be irrelevant.
Let's watch the follow-up StatQuest video to see this in action.
Regularization Part 2: Lasso (L1) Regression
The video 'Regularization Part 2: Lasso (L1) Regression' builds directly on the concepts from the Ridge video.
Watch from the beginning to 07:12. Pay close attention to the section starting at 04:50, which explains the 'big difference' between Ridge and Lasso—Lasso's ability to shrink slopes all the way to zero and thus perform feature selection.
Test your understanding!
You are building a model to predict house prices. You have 100 features, including square_footage, num_bedrooms, and color_of_mailbox. You suspect that color_of_mailbox is completely useless.
If you apply a strong regularization penalty, which technique (Ridge or Lasso) is more likely to give the color_of_mailbox feature a coefficient of exactly zero?
Show answer
Lasso regression is the technique that would likely assign a coefficient of exactly zero to the color_of_mailbox feature. Its L1 penalty is capable of completely eliminating irrelevant features from the model, effectively performing feature selection. Ridge regression, with its L2 penalty, would shrink the coefficient to be very small but would not make it exactly zero.
Applying Ridge and Lasso in Python
Now that we understand the concepts, let's see how to apply them. We'll use the polynomial regression example, which directly ties into our last lesson on the bias-variance tradeoff. By fitting a high-degree polynomial, we'll intentionally create an overfit model and then use Ridge and Lasso to tame it.
The following tutorial provides a great practical walkthrough of this exact process.
A Tutorial on Ridge and Lasso Regression in Python
The article 'A Tutorial on Ridge and Lasso Regression in Python' by Poulami Bakshi provides a clear, code-driven demonstration of these techniques.
Please read the following sections: 'Why Penalize the Magnitude of Coefficients?': This section sets up the problem using polynomial regression, showing how complexity leads to exploding coefficients. This directly connects to our previous lesson. 'Ridge Regression': Follow the explanation and the Python code. Notice how increasing alpha reduces the model complexity and the magnitude of the coefficients. 'Lasso Regression': Follow this section and compare its results to Ridge. Pay special attention to the part where the author checks for zero coefficients and discusses sparsity.
To consolidate this, let's look at a focused code example. Here, we create a dataset with 10 features, but only 2 of them are actually useful for prediction. The rest are just noise. This is a perfect scenario to see the power of Lasso.
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 1. Generate synthetic data
np.random.seed(42)
n_samples = 100
n_features = 10
n_informative = 2 # Only 2 features will actually be useful
# Create a design matrix X
X = np.random.randn(n_samples, n_features)
# Create true coefficients: 2 are large, the rest are zero
true_coef = np.zeros(n_features)
true_coef[:n_informative] = [15, -8]
# Create the target variable y with some noise
y = X @ true_coef + np.random.randn(n_samples) * 5
# Scale the data - this is important for regularization!
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 2. Fit the models
# Ordinary Linear Regression
lr = LinearRegression()
lr.fit(X_scaled, y)
# Ridge Regression (alpha is the regularization strength)
ridge = Ridge(alpha=1.0)
ridge.fit(X_scaled, y)
# Lasso Regression
lasso = Lasso(alpha=1.0)
lasso.fit(X_scaled, y)
# 3. Compare the coefficients
print("True Coefficients:\n", true_coef)
print("\nLinear Regression Coefficients:\n", np.round(lr.coef_, 2))
print("\nRidge Regression Coefficients:\n", np.round(ridge.coef_, 2))
print("\nLasso Regression Coefficients:\n", np.round(lasso.coef_, 2))
Running this code produces the following output:
True Coefficients:
[15. -8. 0. 0. 0. 0. 0. 0. 0. 0.]
Linear Regression Coefficients:
[14.61 -7.71 -0.47 0.37 0.42 0.03 -0.63 -0.59 0.72 -0.82]
Ridge Regression Coefficients:
[13.79 -7.42 -0.42 0.31 0.38 0.06 -0.56 -0.52 0.64 -0.73]
Lasso Regression Coefficients:
[13.48 -6.59 -0. 0. -0. -0. -0. -0. 0. -0. ]
Analysis of the output:
- Linear Regression: It correctly identifies the two important features (with coefficients 14.61 and -7.71), but it also assigns non-zero, noisy coefficients to all the useless features.
- Ridge Regression: It shrinks all the coefficients. The important ones get a bit smaller (13.79, -7.42), and the noisy ones also get a bit smaller. However, none of them become exactly zero.
- Lasso Regression: This is the star of the show. It correctly identifies the two important features (13.48, -6.59) and, crucially, it sets the coefficients for all the useless, noisy features to exactly zero. It has successfully performed feature selection and recovered a model that is much closer to the "true" underlying data-generating process.
Conclusion
In this lesson, we moved from the theory of overfitting to the practical application of regularization. You now have two powerful techniques in your toolkit to build more robust and generalizable models.
Key Takeaways:
- Regularization prevents overfitting by adding a penalty for large coefficients to the model's loss function.
- Ridge Regression (L2) uses a
sum of squarespenalty. It shrinks coefficients towards zero, which is effective for reducing model complexity, especially when many features are somewhat predictive. - Lasso Regression (L1) uses a
sum of absolute valuespenalty. It can shrink coefficients all the way to zero, performing automatic feature selection. This is extremely powerful when you suspect many of your features are irrelevant. - The strength of regularization is controlled by a hyperparameter, , which must be tuned (often via cross-validation) to find the right balance between bias and variance.
Preview of the Next Lesson:
We've now seen what Ridge and Lasso do and how to apply them. But why does the L1 penalty lead to sparsity while the L2 penalty doesn't? In our next lesson, we will compare the effects of L1 and L2 penalties on model coefficients in greater depth. We will explore the geometric intuition behind these methods and analyze their behavior with correlated features, giving you a deeper understanding of when and why to choose one over the other.