Skip to Content
OchreML
Linear, GLM & Linear SVM

Ridge

Linear least squares with L2 (Tikhonov) regularization for stable regression on collinear or high-dimensional features.

Overview

Ridge solves L2-regularized linear least squares by adding a Tikhonov penalty proportional to the squared L2 norm of the weights:

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

By adding alpha * I to the Gram matrix X^T W X (leaving the intercept unpenalized), Ridge guarantees a well-conditioned positive-definite system even when features are highly correlated or n_features > n_samples. Supports 1D and multi-output 2D targets plus sample_weight.

When to Use

  • Continuous regression with multicollinear features where unregularized LinearRegression produces unstable or exploding coefficients.
  • High-dimensional datasets where L2 shrinkage improves generalization without dropping features.
  • Multi-output regression requiring fast closed-form fitting.

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 Ridge
# or top-level lazy import:
from ochreml import Ridge

model = Ridge(
    alpha: float = 1.0,
    fit_intercept: bool = True,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
alphafloat1.0Regularization strength multiplying the L2 penalty term. Must be non-negative; larger values specify stronger shrinkage.
fit_interceptboolTrueWhether to fit an unpenalized intercept term.
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 L2-regularized weight vector or matrix.
intercept_float or ndarray of shape (n_targets,)Independent bias term(s).
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 Ridge regression model with optional sample weights.
predictpredict(X) -> ndarrayPredict continuous values for input matrix X.
scorescore(X, y, sample_weight=None) -> floatReturn coefficient of determination R^2.
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadmodel.save(path) / Ridge.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 Ridge

rng = np.random.default_rng(7)
X = rng.standard_normal((400, 6))
X[:, 1] = X[:, 0] + rng.normal(0.0, 0.01, size=400)  # strong collinearity
y = 3.0 * X[:, 0] - 1.5 * X[:, 2] + 0.5 + rng.normal(0.0, 0.2, size=400)

model = Ridge(alpha=1.0, fit_intercept=True, device="auto")
model.fit(X, y)

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

# Persist to .dit and reload
model.save("ridge_model.dit")
loaded = ochreml.load("ridge_model.dit", device="cpu")
assert np.allclose(preds, loaded.predict(X[:5]))

On this page