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
alphavia an externalGridSearchCVloop. - 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
| 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 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
| Parameter | Type | Default | Description |
|---|---|---|---|
alphas | Sequence[float] or float | (0.1, 1.0, 10.0) | Candidate L2 penalty values to evaluate during cross-validation. |
fit_intercept | bool | True | Whether to fit an unpenalized intercept term. |
cv | int | None | None | Number of cross-validation folds. When None, uses efficient Leave-One-Out cross-validation. |
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 |
|---|---|---|
alpha_ | float | Best regularization strength selected from alphas. |
best_score_ | float | Cross-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_ | 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 | Run cross-validation over alphas and fit the best Ridge model. |
predict | predict(X) -> ndarray | Predict continuous targets using the best fitted model. |
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) / 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]))