Ridge and Lasso
Ridge and Lasso regression regularize linear regression to prevent overfitting by adding norm penalties on the weights. Ridge adds an L2 squared penalty that yields a unique closed-form solution and shrinks correlated weights evenly toward zero, whereas Lasso adds an L1 absolute penalty solved numerically that drives weak coefficients to exact zero for automatic feature selection.
Study Card: Ridge Regression vs. Lasso Regression
Direct Answer
Ridge and Lasso regression constrain linear model complexity by adding a regularization penalty () to least-squares loss:
Key distinctions:
- Shrinkage vs. Sparsity: Ridge () shrinks coefficients continuously toward zero without setting them to exact zero; Lasso () can set irrelevant coefficients to exact zero, performing embedded feature selection.
- Closed-Form vs. Iterative: Ridge admits an analytical closed-form solution that remains invertible for even when features are collinear or . Lasso lacks a general closed-form solution and is solved via coordinate descent, LARS, or proximal gradient methods.
- Correlated Features: Ridge shrinks weights on correlated predictors smoothly together, whereas Lasso tends to select one predictor arbitrarily and zero out the others.
Key Terms
- Regularization: Adding a penalty term on parameter norms (excluding the intercept) to prevent overfitting and stabilize solutions.
- L1 Penalty: , promoting sparse models via the diamond constraint boundary.
- L2 Penalty: , shrinking weights smoothly and resolving ill-conditioned .
- Overfitting: Fitting noise in the training set due to excessive variance or high-dimensional predictors.
- Sparsity: A model representation where many weights equal exact zero.
Example
Consider predicting house prices from standardized features including square footage, neighborhood score, house age, and noisy measurements like front-door color. If many predictors are weakly correlated with price, Ridge shrinks small coefficients down while retaining signal across all predictors. If only a handful of predictors truly drive price and the rest are noise, Lasso zeros out irrelevant features (house age or noise columns) to produce a sparse, interpretable regression equation.
Code Implementation
import numpy as np
from sklearn.linear_model import Ridge, Lasso
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
# Sample data (only features 0 and 1 drive y)
np.random.seed(42)
X = np.random.rand(100, 5)
y = 2 + 3 * X[:, 0] + 1.5 * X[:, 1] + np.random.randn(100) * 0.5
# Split and standardize features (fit scaler only on training data)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Ridge Regression (L2 penalty)
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
ridge_mse = mean_squared_error(y_test, ridge.predict(X_test))
print(f"Ridge Regression MSE: {ridge_mse:.4f}")
print(f"Ridge Coefficients: {np.round(ridge.coef_, 4)}")
# Lasso Regression (L1 penalty)
lasso = Lasso(alpha=0.1)
lasso.fit(X_train, y_train)
lasso_mse = mean_squared_error(y_test, lasso.predict(X_test))
print(f"Lasso Regression MSE: {lasso_mse:.4f}")
print(f"Lasso Coefficients: {np.round(lasso.coef_, 4)}")
Related Concepts
- Elastic Net Regression: Combines and penalties () to retain sparsity while stabilizing grouped correlated features and allowing more than selected variables when .
- Bias-Variance Tradeoff: Increasing increases bias while lowering variance; optimal is selected via cross-validation (
RidgeCV,LassoCV). - Feature Scaling: Standardization is essential before applying Ridge or Lasso so features with smaller numerical units are not penalized more heavily.
- Bayesian MAP Equivalence: Ridge corresponds to a Gaussian weight prior, while Lasso corresponds to a Laplace prior.