Skip to Content
OchreML
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, use LogisticRegression instead.

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 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

ParameterTypeDefaultDescription
alphafloat1.0L2 regularization strength. Larger values enforce stronger weight 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_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_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 classifier on numeric class labels with optional sample weights.
decision_functiondecision_function(X) -> ndarrayCompute raw signed confidence scores per sample (or per class for multiclass).
predictpredict(X) -> ndarrayPredict class labels for samples in X.
scorescore(X, y, sample_weight=None) -> floatReturn mean classification accuracy on (X, y).
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadclf.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]))

On this page