API Reference / algorithms / linear /

linear_regression.py

Ordinary Least Squares (OLS) linear regression with ridge regularization.

Classes

LinearRegression

class algorithms.linear.linear_regression.LinearRegression(Regressor)

Linear Regression for predicting continuous values using ordinary least squares.

Implements ordinary least squares (OLS) linear regression with optional ridge regularization (L2) and automatic feature selection methods including M5 stepwise elimination and greedy backward elimination.
Constructor
__init__(
    self,
    ridge: float = 1e-08,
    attribute_selection: str = 'none',
    eliminate_colinear: bool = True,
    fit_intercept: bool = True,
)

Overview

The algorithm fits a linear model through the following steps:

  1. Handle missing values by replacing with column means
  2. Standardize features to zero mean and unit variance
  3. Optionally eliminate highly colinear features (correlation > 0.99)
  4. Optionally perform feature selection (M5 or greedy backward elimination)
  5. Solve for coefficients using least squares with ridge regularization
  6. Transform coefficients back to the original feature scale

Theory

The model fits a linear equation relating features to the target:

y = X\beta + \epsilon

where \beta is the coefficient vector and \epsilon is the error term. The coefficients are estimated by minimizing the regularized sum of squared residuals:

\hat{\beta} = \arg\min_{\beta} \|y - X\beta\|_2^2 + \lambda \|\beta\|_2^2

The closed-form solution is:

\hat{\beta} = (X^T X + \lambda I)^{-1} X^T y

For numerical stability, the implementation uses np.linalg.lstsq with an augmented matrix rather than explicitly forming the Gram matrix.

Parameters

ridge
float = 1e-8
Ridge regularization parameter (L2 penalty). A small value helps prevent numerical instability in matrix inversion.
attribute_selection
{"none", "m5", "greedy"} = "none"

Feature selection method to use during fitting:

  • "none" - Use all features.
  • "m5" - M5 method (stepwise backward elimination based on AIC).
  • "greedy" - Greedy backward elimination based on coefficient significance.
eliminate_colinear
bool = True
Whether to remove highly colinear attributes before fitting.
fit_intercept
bool = True
Whether to fit an intercept (bias) term.

Attributes

coefficients_
np.ndarray
Regression coefficients of shape (n_features,).
intercept_
float
Intercept term.
selected_features_
np.ndarray
Indices of the features selected for the final model.
std_devs_
np.ndarray
Standard deviations of features used for scaling.
means_
np.ndarray
Means of features used for scaling.

Notes

Complexity:

  • Training: O(n \cdot m^2 + m^3) for n samples and m features.
  • Prediction: O(m) per sample.
When to use LinearRegression:
  • When the relationship between features and target is approximately linear
  • When you need an interpretable model with explicit feature coefficients
  • When the number of features is moderate relative to the number of samples
  • As a baseline for comparison with more complex regression methods

References

Akaike1974
Akaike, H. (1974). A new look at the statistical model identification. IEEE Transactions on Automatic Control, 19(6), 716-723.
HoerlKennard1970
Hoerl, A.E. and Kennard, R.W. (1970). Ridge Regression: Biased Estimation for Nonorthogonal Problems. Technometrics, 12(1), 55-67.

Basic regression with automatic feature handling:

python
>>> import numpy as np
>>> from tuiml.algorithms.linear import LinearRegression
>>>
>>> # Generating sample data
>>> X = np.array([[1], [2], [3], [4]])
>>> y = np.array([2, 4, 6, 8])
>>>
>>> # Fit the model
>>> reg = LinearRegression()
>>> reg.fit(X, y)
>>>
>>> # Predict
>>> reg.predict([[5]])
array([10.])

Methods

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema.

get_capabilities (cls) -> List[str]

Return algorithm capabilities.

get_complexity (cls) -> str

Return time/space complexity.

get_references (cls) -> List[str]

Return academic references.

fit (self, X: np.ndarray, y: np.ndarray) -> 'LinearRegression'

Fit the Linear Regression model.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Training features.
y
np.ndarray of shape (n_samples,)
Training target values.
Returns
self
LinearRegression
Fitted estimator.
predict (self, X: np.ndarray) -> np.ndarray

Predict target values for samples.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Samples to predict.
Returns
predictions
np.ndarray of shape (n_samples,)
Predicted continuous values.
score (self, X: np.ndarray, y: np.ndarray) -> float

Compute the R-squared (coefficient of determination) score.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Test features.
y
np.ndarray of shape (n_samples,)
True target values.
Returns
score
float
R-squared score.
__repr__ (self) -> str

String representation.