Skip to content

Linear regression

Module: linear_regression.py

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

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

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

Classes:

Name Description
LinearRegressionModel

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

LinearRegressionModel

Bases: Regression

Linear Regression model implementation using scikit-learn.

Source code in scirex/core/ml/supervised/regression/linear_regression.py
class LinearRegressionModel(Regression):
    """
    Linear Regression model implementation using scikit-learn.
    """

    def __init__(self, random_state: int = 42) -> None:
        """
        Initialize the LinearRegressionModel class.

        Args:
            random_state (int, optional): Seed for reproducibility where applicable.
                                          Defaults to 42.
        """
        super().__init__(model_type="linear_regression", random_state=random_state)
        self.model = LinearRegression()

    def fit(self, X: np.ndarray, y: np.ndarray) -> None:
        """
        Fit the linear regression model to the 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 data (n_samples, n_features).

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

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

        Returns:
            Dict[str, Any]: The parameters of the linear regression model.
        """
        return {
            "coefficients": self.model.coef_,
            "intercept": self.model.intercept_,
        }

__init__(random_state=42)

Initialize the LinearRegressionModel class.

Parameters:

Name Type Description Default
random_state int

Seed for reproducibility where applicable. Defaults to 42.

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

    Args:
        random_state (int, optional): Seed for reproducibility where applicable.
                                      Defaults to 42.
    """
    super().__init__(model_type="linear_regression", random_state=random_state)
    self.model = LinearRegression()

fit(X, y)

Fit the linear regression model to the 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/linear_regression.py
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
    """
    Fit the linear regression model to the 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 linear regression model.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: The parameters of the linear regression model.

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

    Returns:
        Dict[str, Any]: The parameters of the linear regression 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 data (n_samples, n_features).

required

Returns:

Type Description
ndarray

np.ndarray: The predicted target values.

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

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

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