Study Card: Ridge Regression vs. Lasso Regression

Direct Answer

Ridge and Lasso regression are both regularization techniques used to prevent overfitting in linear regression models by adding a penalty term to the cost function. Ridge regression adds a penalty proportional to the sum of squares of the coefficients (L2 penalty), which shrinks coefficients towards zero but doesn't eliminate them. Lasso regression adds a penalty proportional to the absolute value of the coefficients (L1 penalty), which can shrink some coefficients to zero, effectively performing feature selection. The key difference lies in the type of penalty, leading to different effects on model coefficients and sparsity.

Key Terms

Example

Consider predicting house prices based on various features like size, location, number of bedrooms, etc. Using a simple linear regression might overfit if the dataset is small or has many features. Ridge regression could help by shrinking the coefficients of less important features, reducing their impact on the prediction. Lasso regression could further improve the model by setting the coefficients of truly irrelevant features to zero, effectively selecting the most important features and creating a sparser, more interpretable model. For instance, if 'house age' and 'distance to nearest school' are less important, Lasso might eliminate them.

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
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 data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Scale the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Ridge Regression
ridge = Ridge(alpha=1.0)  # alpha is the regularization strength
ridge.fit(X_train, y_train)
ridge_predictions = ridge.predict(X_test)
ridge_mse = mean_squared_error(y_test, ridge_predictions)
print(f"Ridge Regression MSE: {ridge_mse}")
print(f"Ridge Coefficients: {ridge.coef_}")

# Lasso Regression
lasso = Lasso(alpha=0.1)  # alpha is the regularization strength
lasso.fit(X_train, y_train)
lasso_predictions = lasso.predict(X_test)
lasso_mse = mean_squared_error(y_test, lasso_predictions)
print(f"Lasso Regression MSE: {lasso_mse}")
print(f"Lasso Coefficients: {lasso.coef_}")

Related Concepts