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^2By 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
LinearRegressionproduces 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
| Backend | Primitives Used | Support |
|---|---|---|
| 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
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | Regularization strength multiplying the L2 penalty term. Must be non-negative; larger values specify stronger shrinkage. |
fit_intercept | bool | True | Whether to fit an unpenalized intercept term. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
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_ | int | Number of features seen during fit. |
device_ | str | Resolved hardware device string used by the estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> self | Fit Ridge regression model with optional sample weights. |
predict | predict(X) -> ndarray | Predict continuous values for input matrix X. |
score | score(X, y, sample_weight=None) -> float | Return coefficient of determination R^2. |
to | to(device: str) -> self | Move the estimator execution target to another hardware device. |
save / load | model.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]))