MLP FU
Models/Regression

Lasso Regression

Where to Use Lasso Regression

Lasso regression is useful when you want to both regularize your model and perform feature selection (make some weights exactly zero).

Common use cases:

  • Predicting with many features, some of which may not be useful
  • When you want a simpler model that ignores unimportant features
  • Reducing overfitting and improving interpretability

Why Use Lasso Regression?

  • Feature selection: Can set some weights to zero, removing unimportant features
  • Reduces overfitting: Adds a penalty to large weights
  • Simple and interpretable

How to Use Lasso 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 Lasso 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 Lasso Regression Work?

Lasso regression is like linear regression, but it adds a penalty for large weights and can set some weights to zero. This penalty is controlled by the alpha parameter.


Step-by-Step Example

from sklearn.linear_model import Lasso
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 Lasso regression
lasso = Lasso(alpha=1.0)
lasso.fit(X_train, y_train)

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

Explanation:

  • Lasso(alpha=1.0): Creates a Lasso 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('Lasso weights:', lasso.coef_)
print('Lasso intercept:', lasso.intercept_)

Explanation:

  • coef_: The weights for each feature. Some may be exactly zero.
  • intercept_: The bias term.

Visualizing Lasso Regression