Skip to content

Ridge regression

Module: ridge_regression.py

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

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

Key Features
  • Support for fitting a ridge regression model
  • Access to model parameters (coefficients, intercept)
  • Seamless integration with the SciREX regression pipeline

Classes:

Name Description
RidgeRegressionModel

Implements a Ridge 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

RidgeRegressionModel

Bases: Regression

Ridge Regression model implementation using scikit-learn.

This model performs Ridge Regression, which is a linear model that uses L2 regularization. Ridge regression is useful when there is multicollinearity among input features or when there are more predictors than observations.

Attributes:

Name Type Description
model Ridge

A scikit-learn Ridge model.

Source code in scirex/core/ml/supervised/regression/ridge_regression.py
class RidgeRegressionModel(Regression):
    """
    Ridge Regression model implementation using scikit-learn.

    This model performs Ridge Regression, which is a linear model that uses L2 regularization.
    Ridge regression is useful when there is multicollinearity among input features or when
    there are more predictors than observations.

    Attributes:
        model (Ridge): A scikit-learn Ridge model.
    """

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

        Args:
            alpha (float, optional): Regularization strength; must be a positive float.
                                      Defaults to 1.0.
            random_state (int, optional): Seed for reproducibility where applicable.
                                          Defaults to 42.
        """
        super().__init__(model_type="ridge_regression", random_state=random_state)
        self.model = Ridge(alpha=alpha, random_state=random_state)

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

        Args:
            X (np.ndarray): The input training features (n_samples, n_features).
            y (np.ndarray): The target training values (n_samples).

        Returns:
            None
        """
        self.model.fit(X, y)

    def predict(self, X: np.ndarray) -> np.ndarray:
        """
        Predict the target values for the input data.

        Args:
            X (np.ndarray): The input features (n_samples, n_features).

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

    def get_model_params(self) -> Dict[str, Any]:
        """
        Get the parameters of the Ridge regression model.

        Returns:
            Dict[str, Any]: A dictionary containing the model's parameters:
                - "coefficients": The coefficients (weights) of the linear model.
                - "intercept": The intercept term of the linear model.
        """
        return {
            "coefficients": self.model.coef_,
            "intercept": self.model.intercept_,
        }

__init__(alpha=1.0, random_state=42)

Initialize the RidgeRegressionModel class.

Parameters:

Name Type Description Default
alpha float

Regularization strength; must be a positive float. Defaults to 1.0.

1.0
random_state int

Seed for reproducibility where applicable. Defaults to 42.

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

    Args:
        alpha (float, optional): Regularization strength; must be a positive float.
                                  Defaults to 1.0.
        random_state (int, optional): Seed for reproducibility where applicable.
                                      Defaults to 42.
    """
    super().__init__(model_type="ridge_regression", random_state=random_state)
    self.model = Ridge(alpha=alpha, random_state=random_state)

fit(X, y)

Fit the Ridge regression model to the input data.

Parameters:

Name Type Description Default
X ndarray

The input training features (n_samples, n_features).

required
y ndarray

The target training values (n_samples).

required

Returns:

Type Description
None

None

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

    Args:
        X (np.ndarray): The input training features (n_samples, n_features).
        y (np.ndarray): The target training values (n_samples).

    Returns:
        None
    """
    self.model.fit(X, y)

get_model_params()

Get the parameters of the Ridge regression model.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary containing the model's parameters: - "coefficients": The coefficients (weights) of the linear model. - "intercept": The intercept term of the linear model.

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

    Returns:
        Dict[str, Any]: A dictionary containing the model's parameters:
            - "coefficients": The coefficients (weights) of the linear model.
            - "intercept": The intercept term of the linear model.
    """
    return {
        "coefficients": self.model.coef_,
        "intercept": self.model.intercept_,
    }

predict(X)

Predict the target values for the input data.

Parameters:

Name Type Description Default
X ndarray

The input features (n_samples, n_features).

required

Returns:

Type Description
ndarray

np.ndarray: The predicted target values (n_samples,).

Source code in scirex/core/ml/supervised/regression/ridge_regression.py
def predict(self, X: np.ndarray) -> np.ndarray:
    """
    Predict the target values for the input data.

    Args:
        X (np.ndarray): The input features (n_samples, n_features).

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