Skip to Content
OchreML
Linear, GLM & Linear SVM

LinearRegression

Ordinary Least Squares (OLS) linear regression supporting single-output, multi-output, and weighted fitting across CPU, CUDA, wgpu, and TPU.

Overview

LinearRegression fits a linear model by minimizing the residual sum of squares between observed targets and linear predictions:

min ||y - (X @ w + b)||_2^2

OchreML solves the Normal Equation (X^T W X) w = X^T W y using Cholesky decomposition with automatic fallback for ill-conditioned matrices and zero-copy Python Buffer Protocol ingestion. It supports both 1D targets (n_samples,) and 2D multi-output targets (n_samples, n_targets), as well as optional sample_weight.

When to Use

  • Baseline regression tasks with a linear relationship between features and continuous targets.
  • Multi-output regression where multiple correlated or independent target columns are predicted simultaneously.
  • Large dense tabular matrices where hardware-accelerated Gram matrix construction (X^T X) provides significant speedups.
  • When features are strongly collinear or n_features > n_samples, prefer Ridge or RidgeCV.

Hardware Backend Support

BackendPrimitives UsedSupport
CPU (Rayon)Dense E1-E5 (f64)Full (default fallback)
CUDA (cuda:0, cuda:all)cuBLAS FP64 Gram & matvec (E1-E5)Full (including multi-GPU row sharding)
wgpu (wgpu)Split-K + Kahan WGSL compute shaders (E1-E5)Full
TPU (tpu:0)OpenXLA PJRT StableHLO dot_general (E1-E5)Full

Import & Constructor

from ochreml.linear_model import LinearRegression
# or top-level lazy import:
from ochreml import LinearRegression

model = LinearRegression(
    fit_intercept: bool = True,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
fit_interceptboolTrueWhether to calculate the intercept (b) for this model. If False, data is assumed to be centered.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
coef_ndarray of shape (n_features,) or (n_targets, n_features)Estimated linear weights.
intercept_float or ndarray of shape (n_targets,)Independent bias term(s). 0.0 when fit_intercept=False.
n_features_in_intNumber of features seen during fit.
device_strResolved hardware device string used by the estimator.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> selfFit OLS model on 1D or 2D targets with optional sample weights.
predictpredict(X) -> ndarrayPredict continuous values using the fitted linear model.
scorescore(X, y, sample_weight=None) -> floatReturn coefficient of determination R^2 (uniform average across targets for multi-output).
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadmodel.save(path) / LinearRegression.load(path, device=None)Persist or restore the model in OchreML's binary .dit format.

Usage Example

import numpy as np
import ochreml
from ochreml.linear_model import LinearRegression

rng = np.random.default_rng(42)
X = rng.standard_normal((500, 4))
true_w = np.array([2.5, -1.2, 0.8, 3.0])
y = X @ true_w + 1.5 + rng.normal(0.0, 0.1, size=500)

model = LinearRegression(fit_intercept=True, device="auto")
model.fit(X, y)

preds = model.predict(X[:5])
r2 = model.score(X, y)
print(f"R^2: {r2:.4f}, coef_: {model.coef_.round(3)}, intercept_: {model.intercept_:.3f}")

# Save and reload with .dit persistence
model.save("ols_model.dit")
loaded = ochreml.load("ols_model.dit", device="cpu")
assert np.allclose(preds, loaded.predict(X[:5]))

On this page