Course contentsShow
← Machine Learning and Deep Learning
Lesson 233 of 3,5383. Linear RegressionFree lesson

Implementing Ridge and Lasso in Scikit-learn

Using Ridge, Lasso, and ElasticNet classes with cross-validated hyperparameter tuning.

Implementing Ridge and Lasso in Scikit-learn

What you'll learn: How to apply Ridge, Lasso, and ElasticNet regression in Scikit-learn with automatic hyperparameter tuning through cross-validation.

From Theory to Practice

You've learned the mathematics behind Ridge (L2), Lasso (L1), and Elastic Net regularization. Now it's time to use them efficiently. Scikit-learn provides not just the basic classes (Ridge, Lasso, ElasticNet), but also cross-validated versions that automatically find the best regularization parameter for you.

The Three Approaches

Basic Implementation

Use Ridge, Lasso, or ElasticNet when you already know your regularization parameter (alpha):

from sklearn.linear_model import Ridge, Lasso, ElasticNet

ridge = Ridge(alpha=1.0)
lasso = Lasso(alpha=0.1)
elastic = ElasticNet(alpha=0.5, l1_ratio=0.5)  # l1_ratio balances L1/L2

Cross-Validated Versions (Recommended)

Use RidgeCV, LassoCV, or ElasticNetCV to test multiple alpha values automatically:

from sklearn.linear_model import RidgeCV, LassoCV, ElasticNetCV

# Tests multiple alphas with cross-validation
ridge_cv = RidgeCV(alphas=[0.1, 1.0, 10.0, 100.0], cv=5)
lasso_cv = LassoCV(cv=5)  # Automatically generates alphas
elastic_cv = ElasticNetCV(l1_ratio=[0.1, 0.5, 0.9], cv=5)

ridge_cv.fit(X_train, y_train)
print(f"Best alpha: {ridge_cv.alpha_}")  # Access chosen parameter

Why Use CV Versions?

Think of it like test-driving cars with different engines before buying. The CV versions try each regularization strength, measure performance through cross-validation, and automatically select the best one—all in a single fit() call.

Key Takeaway: Use RidgeCV, LassoCV, and ElasticNetCV for automatic hyperparameter selection via cross-validation, avoiding manual grid search while finding optimal regularization strength efficiently.