MLP FU
Models/Regression

Ridge Regression

Where to Use Ridge Regression

Ridge regression is useful when you have many features and want to prevent overfitting. It adds a penalty to large weights, making the model more stable.

Common use cases:

  • Predicting with many correlated features
  • When linear regression overfits (performs well on training but poorly on test data)
  • Regularizing models to improve generalization

Why Use Ridge Regression?

  • Reduces overfitting: Adds a penalty to large weights
  • Works with many features: Handles multicollinearity (when features are correlated)
  • Simple and interpretable

How to Use Ridge Regression

  1. Prepare your data: Features (X) and target (y) should be numbers.
  2. Split your data: Use train_test_split.
  3. Create and train the model: Use Ridge from scikit-learn.
  4. Make predictions: Use .predict().
  5. Evaluate: Use metrics like mean squared error.

What are the Inputs and Outputs?

  • Input (X): Table of numbers (features).
  • Output (y): A single number for each sample.
  • Prediction: The model outputs a number for each input row.

How Does Ridge Regression Work?

Ridge regression is like linear regression, but it adds a penalty for large weights. This penalty is controlled by the alpha parameter.


Step-by-Step Example

from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np

# Example data
y = np.array([1, 2, 3, 4, 5])
X = np.arange(5).reshape(-1, 1)

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# Create and fit Ridge regression
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)

# Predict and evaluate
preds = ridge.predict(X_test)
mse = mean_squared_error(y_test, preds)
print('Predictions:', preds)
print('MSE:', mse)

Explanation:

  • Ridge(alpha=1.0): Creates a Ridge regression model. alpha controls the strength of the penalty.
  • fit(X_train, y_train): Trains the model.
  • predict(X_test): Makes predictions.
  • mean_squared_error(y_test, preds): Measures prediction error.

Accessing Model Weights

print('Ridge weights:', ridge.coef_)
print('Ridge intercept:', ridge.intercept_)

Explanation:

  • coef_: The weights for each feature.
  • intercept_: The bias term.

Visualizing Ridge Regression