Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 24

Ridge and Lasso Regression: Regularization in Practice

Recommended resources

Welcome back! In our previous lessons, we built a solid theoretical foundation for regularized linear models. We started by understanding the core ideas behind Ridge (L2) and Lasso (L1) regularization, and then introduced Elastic Net as a powerful hybrid. We concluded by establishing a decision framework for choosing the right regularization technique for a given problem.

Today, we transition from theory to practice. This lesson is all about applying Ridge and Lasso regression to a real-world dataset using Python's scikit-learn library. You will see firsthand how the alpha hyperparameter controls the regularization strength and how to interpret the resulting impact on model coefficients. This practical application will solidify your understanding of how these techniques combat overfitting and, in the case of Lasso, perform feature selection.

The Practical Workflow: From Data to Interpretation

Before diving into the code, let's outline the key steps we will follow. This workflow is standard for applying regularized models:

  1. Load and Prepare Data: We'll import a dataset and perform some initial exploration.
  2. Split and Scale: We'll split the data into training and testing sets. Crucially, we will then scale our features. This is a vital step for regularization, as the penalties are applied directly to the coefficients. If features are on vastly different scales (e.g., one from 0-1 and another from 10,000-50,000), the penalty would unfairly punish the coefficients of the larger-scaled features. Standardization ensures all features are treated equally by the regularization term.
  3. Train a Baseline Model: We'll fit a standard LinearRegression model to serve as our baseline for comparison.
  4. Train Regularized Models: We'll fit Ridge and Lasso models to the same data.
  5. Compare and Interpret: The final and most important step is to compare the coefficients and performance scores of all three models to understand the distinct effects of L1 and L2 regularization.

We will use a comprehensive tutorial from DataCamp as our guide for this practical exercise. It provides all the necessary code and context using the Boston housing dataset, a classic dataset for regression tasks.

Lasso and Ridge Regression in Python Tutorial

This tutorial provides a step-by-step implementation of Ridge and Lasso regression. We'll start by setting up our data.

Begin by reviewing the code under the heading Data Importation and EDA. This part handles loading the necessary libraries and the dataset. Next, focus on the section Data Splitting and Scaling. Pay close attention to the use of StandardScaler. As we discussed, scaling is essential before applying regularization. This code block prepares the X_train and X_test sets that we will use for the rest of the lesson.


Applying Ridge Regression and Observing Shrinkage

With our data prepared, our first step is to train a standard linear regression model and a Ridge regression model. This will allow us to directly compare their coefficients and see the "shrinking" effect of the L2 penalty in action.

Lasso and Ridge Regression in Python Tutorial

This section of the tutorial implements and compares the Linear and Ridge regression models.

Please read the entire section Linear and Ridge Regression Models. As you review the code, take note of the following: A standard LinearRegression model is fit first to establish a baseline. A Ridge model is then fit with alpha=10. This alpha value determines the strength of the L2 penalty. The tutorial then plots the coefficients from both models. Examine this plot closely. You should see that the red markers (Ridge coefficients) are generally closer to zero than the green markers (Linear Regression coefficients). This is the visual evidence of coefficient shrinkage.

The comparison plot you just examined is the key to interpretation. The unregularized linear regression model may have large coefficient values, suggesting it's placing a great deal of importance on certain features. This can be a sign of overfitting, where the model is too closely tuned to the noise in the training data. Ridge regression mitigates this by "pulling" all coefficients towards zero, resulting in a less complex model that often generalizes better to unseen data.


Applying Lasso Regression and Witnessing Feature Selection

Now, let's turn our attention to Lasso (L1) regression. While it also shrinks coefficients, its main distinguishing feature is its ability to shrink some of them to exactly zero. This effectively removes the corresponding features from the model, performing automatic feature selection.

Let's see what happens when we apply Lasso with a strong penalty.

Lasso and Ridge Regression in Python Tutorial

This next section implements the Lasso model.

Read the code under the heading Lasso Regression. The tutorial again uses alpha = 10. Notice the model scores—they are terrible! The bar chart of the coefficients reveals why: the penalty was so strong that all coefficients were forced to zero. This is an extreme but powerful demonstration of Lasso's feature selection capability.

This result perfectly illustrates the double-edged nature of the alpha hyperparameter. A value that is too high can lead to an over-penalized, underfitting model. This begs the question: how do we find the optimal value for alpha?


Visualizing the Regularization Path

Before we answer that question, it's helpful to visualize what happens to the coefficients as we vary the regularization strength. A "regularization path" plot shows the magnitude of each feature's coefficient as a function of alpha.

This plot shows the "regularization path" for a Ridge model. Each colored line represents the coefficient for a single feature. As alpha decreases (moving from right to left), the regularization strength weakens, and the coefficients grow in magnitude away from zero. A high alpha (right side) heavily constrains the coefficients, keeping them small.

This visualization makes the trade-off clear. A very high alpha (right side of the plot) results in a simple model with small coefficients. As alpha decreases, the model becomes more complex, allowing the coefficients to take on larger values that better fit the training data. Our goal is to find the "sweet spot" that balances this complexity to achieve the best performance on unseen data.


Finding the Optimal Alpha with Cross-Validation

Manually testing different values for alpha is tedious and unreliable. The standard, robust method for tuning this hyperparameter is to use cross-validation. Scikit-learn provides convenient classes, LassoCV and RidgeCV, that automatically perform this process for us. These models test a range of alpha values using cross-validation and select the one that performs best.

Let's see this in action with LassoCV.

Lasso and Ridge Regression in Python Tutorial

This final section demonstrates how to use cross-validation to find the optimal alpha and provides a final comparison.

First, read the code and explanation in the section Selecting Optimal Alpha Values. Notice how LassoCV is used to test a list of potential alphas and how the resulting model scores are much better than our previous attempt with a manually chosen alpha. Finally, examine the concluding plot in the tutorial, found under the comment Comparison plot. This plot brings everything together, showing the coefficients for: Linear Regression (green): The original, unconstrained coefficients. Ridge (red): All coefficients are shrunk towards zero. LassoCV (blue): Some coefficients are shrunk, while others have been forced to exactly zero, effectively removing those features from the model.

This final plot is the culmination of our work. By comparing the three sets of coefficients, you can concretely interpret the impact of each regularization strategy on the model.

To connect this practical result back to the theory, let's briefly revisit the geometric interpretation.

This image shows the geometric constraints for Lasso (a diamond) and Ridge (a circle). The optimal coefficients are found where the elliptical contours of the loss function first touch the constraint region.

The coefficient plots you just generated are a direct consequence of this geometry.

  • Ridge's circular constraint means that the loss function's ellipse will almost always touch the boundary at a point where no coefficient is zero. This leads to shrinkage without feature elimination.
  • Lasso's diamond-shaped constraint has sharp corners that lie on the axes. The ellipse often makes its first contact at one of these corners, forcing the coefficient for the other axis to become exactly zero. This is the geometric basis for Lasso's feature selection property.

For a final reinforcement of this geometric intuition, you can review the following short explanation.

Ridge and Lasso Regression: L1 and L2 Regularization

This section provides a concise geometric explanation for why Lasso performs feature selection and Ridge does not.

Read the part under the heading How Lasso Regularization Leads to Feature Selection?. This nicely ties the visual theory to the practical results you've seen in the code.


Conclusion

In this lesson, you successfully bridged the gap between the theory of regularization and its practical application. You used scikit-learn to apply Ridge and Lasso regression to a dataset, seeing firsthand how they modify model coefficients to combat overfitting.

Key Takeaways:

  • Feature scaling is a mandatory preprocessing step before applying regularized models.
  • Ridge (L2) regression shrinks the coefficients of all features, reducing model complexity and helping with multicollinearity. All features remain in the model.
  • Lasso (L1) regression also shrinks coefficients but has the unique ability to force some to exactly zero, performing automatic feature selection.
  • The alpha hyperparameter controls the strength of the penalty. A value that is too large can cause underfitting.
  • RidgeCV and LassoCV are the standard tools for efficiently finding the optimal alpha value using cross-validation.
  • By plotting and comparing coefficients, you can directly interpret the impact of regularization and gain insights into which features are being prioritized or eliminated.

In our next lesson, we will complete our practical exploration by applying Elastic Net regression. We will then compare its performance against Ridge, Lasso, and standard linear regression, solidifying your ability to choose and apply the best model for a given scenario.