Linear, GLM & Linear SVM
RidgeClassifier
Fast closed-form linear classifier using Ridge regression on {-1, +1} encoded class targets.
Overview
RidgeClassifier converts target labels into {-1, +1} numeric targets (one column for binary classification, one-vs-rest matrix for multiclass) and solves the resulting L2-regularized least-squares problem in closed form. Because it avoids iterative optimization, it is often significantly faster to train than LogisticRegression on large multiclass problems while maintaining strong linear decision boundaries.
When to Use
- Fast baseline linear classification for binary or multiclass datasets.
- High-dimensional feature matrices where iterative solvers converge slowly and calibrated class probabilities are not required.
- When calibrated probabilities (
predict_proba) are required, useLogisticRegressioninstead.
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 RidgeClassifier
# or top-level lazy import:
from ochreml import RidgeClassifier
clf = RidgeClassifier(
alpha: float = 1.0,
fit_intercept: bool = True,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | L2 regularization strength. Larger values enforce stronger weight 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_classes, n_features) | Weight vector (binary) or matrix (multiclass). |
intercept_ | float or ndarray of shape (n_classes,) | Bias term(s) for decision boundaries. |
classes_ | ndarray of shape (n_classes,) | Sorted unique class labels discovered during fit. |
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 classifier on numeric class labels with optional sample weights. |
decision_function | decision_function(X) -> ndarray | Compute raw signed confidence scores per sample (or per class for multiclass). |
predict | predict(X) -> ndarray | Predict class labels for samples in X. |
score | score(X, y, sample_weight=None) -> float | Return mean classification accuracy on (X, y). |
to | to(device: str) -> self | Move the estimator execution target to another hardware device. |
save / load | clf.save(path) / RidgeClassifier.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 RidgeClassifier
rng = np.random.default_rng(12)
X0 = rng.normal(loc=-1.5, scale=0.8, size=(150, 4))
X1 = rng.normal(loc=1.5, scale=0.8, size=(150, 4))
X = np.vstack([X0, X1])
y = np.array([0] * 150 + [1] * 150, dtype=np.float64)
clf = RidgeClassifier(alpha=1.0, fit_intercept=True, device="auto")
clf.fit(X, y)
scores = clf.decision_function(X[:5])
preds = clf.predict(X[:5])
print(f"Accuracy: {clf.score(X, y):.4f}, classes_: {clf.classes_}")
# Persist to .dit and reload
clf.save("ridge_clf.dit")
loaded = ochreml.load("ridge_clf.dit", device="cpu")
assert np.array_equal(preds, loaded.predict(X[:5]))