Skip to content

Lasso regression

Module: lasso_regression.py

This module implements the LassoRegressionModel class for Lasso regression tasks, extending the base Regression class from the SciREX library.

The implementation uses scikit-learn's Lasso regression model for: - Fitting the Lasso regression algorithm - Generating predictions for input features - Evaluating performance using standard regression metrics

Classes:

Name Description
LassoRegressionModel

Implements a Lasso Regression model.

Dependencies
  • numpy
  • scikit-learn
  • scirex.core.ml.supervised.regression.base
Authors
  • Paranidharan (paranidharan@iisc.ac.in)
Version Info
  • 16/Jan/2025: Initial version

LassoRegressionModel

Bases: Regression

Class: LassoRegressionModel

This class implements a Lasso Regression model for regression tasks. The model is based on scikit-learn's Lasso regression implementation.

The class provides methods for
  • Training the Lasso regression model
  • Generating predictions for input features
  • Evaluating performance using standard regression metrics

Attributes:

Name Type Description
- model

Lasso regression model

Source code in scirex/core/ml/supervised/regression/lasso_regression.py
class LassoRegressionModel(Regression):
    """
    Class: LassoRegressionModel

    This class implements a Lasso Regression model for regression tasks.
    The model is based on scikit-learn's Lasso regression implementation.

    The class provides methods for:
        - Training the Lasso regression model
        - Generating predictions for input features
        - Evaluating performance using standard regression metrics

    Attributes:
        - model: Lasso regression model
    """

    def __init__(self, alpha: float = 1.0, random_state: int = 42) -> None:
        """
        Initialize the LassoRegressionModel class.

        Args:
            alpha (float, optional): Regularization strength. Defaults to 1.0.
            random_state (int, optional): Seed for reproducibility. Defaults to 42.
        """
        super().__init__(model_type="lasso_regression", random_state=random_state)
        self.model = Lasso(alpha=alpha, random_state=random_state)

    def fit(self, X: np.ndarray, y: np.ndarray) -> None:
        """
        Fit the Lasso regression model to the data.

        Args:
            X (np.ndarray): Input features for training (n_samples, n_features).
            y (np.ndarray): Target values for training (n_samples).
        """
        self.model.fit(X, y)

    def predict(self, X: np.ndarray) -> np.ndarray:
        """
        Generate predictions using the trained Lasso regression model.

        Args:
            X (np.ndarray): Input features for prediction (n_samples, n_features).

        Returns:
            np.ndarray: Predicted target values (n_samples,).
        """
        return self.model.predict(X)

    def get_model_params(self) -> Dict[str, Any]:
        """
        Get the model parameters coefficients and intercept.

        Returns:
            Dict[str, Any]: Model parameters, including coefficients and intercept.
        """
        return {
            "coefficients": self.model.coef_,
            "intercept": self.model.intercept_,
        }

__init__(alpha=1.0, random_state=42)

Initialize the LassoRegressionModel class.

Parameters:

Name Type Description Default
alpha float

Regularization strength. Defaults to 1.0.

1.0
random_state int

Seed for reproducibility. Defaults to 42.

42
Source code in scirex/core/ml/supervised/regression/lasso_regression.py
def __init__(self, alpha: float = 1.0, random_state: int = 42) -> None:
    """
    Initialize the LassoRegressionModel class.

    Args:
        alpha (float, optional): Regularization strength. Defaults to 1.0.
        random_state (int, optional): Seed for reproducibility. Defaults to 42.
    """
    super().__init__(model_type="lasso_regression", random_state=random_state)
    self.model = Lasso(alpha=alpha, random_state=random_state)

fit(X, y)

Fit the Lasso regression model to the data.

Parameters:

Name Type Description Default
X ndarray

Input features for training (n_samples, n_features).

required
y ndarray

Target values for training (n_samples).

required
Source code in scirex/core/ml/supervised/regression/lasso_regression.py
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
    """
    Fit the Lasso regression model to the data.

    Args:
        X (np.ndarray): Input features for training (n_samples, n_features).
        y (np.ndarray): Target values for training (n_samples).
    """
    self.model.fit(X, y)

get_model_params()

Get the model parameters coefficients and intercept.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Model parameters, including coefficients and intercept.

Source code in scirex/core/ml/supervised/regression/lasso_regression.py
def get_model_params(self) -> Dict[str, Any]:
    """
    Get the model parameters coefficients and intercept.

    Returns:
        Dict[str, Any]: Model parameters, including coefficients and intercept.
    """
    return {
        "coefficients": self.model.coef_,
        "intercept": self.model.intercept_,
    }

predict(X)

Generate predictions using the trained Lasso regression model.

Parameters:

Name Type Description Default
X ndarray

Input features for prediction (n_samples, n_features).

required

Returns:

Type Description
ndarray

np.ndarray: Predicted target values (n_samples,).

Source code in scirex/core/ml/supervised/regression/lasso_regression.py
def predict(self, X: np.ndarray) -> np.ndarray:
    """
    Generate predictions using the trained Lasso regression model.

    Args:
        X (np.ndarray): Input features for prediction (n_samples, n_features).

    Returns:
        np.ndarray: Predicted target values (n_samples,).
    """
    return self.model.predict(X)