Skip to Content
OchreML
Linear, GLM & Linear SVM

RidgeCV

Ridge regression with built-in cross-validation to automatically select the optimal L2 regularization strength alpha.

Overview

RidgeCV evaluates a candidate grid of L2 regularization strengths (alphas) using built-in cross-validation (efficient Leave-One-Out generalized cross-validation when cv=None, or K-Fold cross-validation when cv is an integer), selects the best alpha_, and refits the final Ridge model on the full dataset.

When to Use

  • Whenever you want Ridge regression without manually tuning alpha via an external GridSearchCV loop.
  • Collinear or noisy tabular regression datasets where the optimal regularization strength spans several orders of magnitude.
  • Supports both 1D targets and 2D multi-output targets as well as sample_weight.

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

model = RidgeCV(
    alphas: tuple[float, ...] = (0.1, 1.0, 10.0),
    fit_intercept: bool = True,
    cv: int | None = None,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
alphasSequence[float] or float(0.1, 1.0, 10.0)Candidate L2 penalty values to evaluate during cross-validation.
fit_interceptboolTrueWhether to fit an unpenalized intercept term.
cvint | NoneNoneNumber of cross-validation folds. When None, uses efficient Leave-One-Out cross-validation.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
alpha_floatBest regularization strength selected from alphas.
best_score_floatCross-validation score achieved by alpha_.
coef_ndarray of shape (n_features,) or (n_targets, n_features)Fitted linear coefficients using alpha_.
intercept_float or ndarray of shape (n_targets,)Fitted intercept 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) -> selfRun cross-validation over alphas and fit the best Ridge model.
predictpredict(X) -> ndarrayPredict continuous targets using the best fitted model.
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) / RidgeCV.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 RidgeCV

rng = np.random.default_rng(21)
X = rng.standard_normal((300, 8))
y = X @ np.array([1.5, -0.8, 0.4, 0.0, 2.1, -1.0, 0.2, 0.5]) + rng.normal(0.0, 0.5, size=300)

model = RidgeCV(alphas=(0.01, 0.1, 1.0, 10.0, 100.0), cv=5, device="auto")
model.fit(X, y)

print(f"Selected alpha_: {model.alpha_}, best_score_: {model.best_score_:.4f}")
preds = model.predict(X[:5])

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

On this page