Skip to Content
OchreML
Kernel, MLP & Gaussian Process

KernelRidge

Kernel Ridge Regression combining L2-regularized least squares with the kernel trick and closed-form dual solving.

Overview

KernelRidge combines Ridge Regression (L2-regularized linear least squares) with the kernel trick, learning a linear function in the reproducing kernel Hilbert space induced by the selected kernel. Unlike SVR (which solves a sparse dual QP via SMO), KernelRidge solves the closed-form dual system (K + alpha * I) * dual_coef = y directly and supports both 1D and 2D multi-output targets as well as sample_weight. (^▽^)

Backend Support

  • Primitives: N3 (pairwise L2 distance for RBF kernels), E1 (dense matrix multiplication for Gram matrices and prediction)
  • Devices: cpu (Rayon parallel f64), cuda / cuda:0 (dynamic cuBLAS), wgpu (Vulkan/Metal/DX12), tpu (OpenXLA PJRT), and auto
  • Persistence: .dit binary format via save() / load() (and ochreml.save() / ochreml.load()), plus Python pickle support

Import & Signature

from ochreml.kernel_ridge import KernelRidge
# Also available from top-level: from ochreml import KernelRidge

KernelRidge(
    alpha: float = 1.0,
    kernel: str = "linear",
    gamma: float | None = None,
    degree: int = 3,
    coef0: float = 1.0,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
alphafloat1.0Regularization strength; must be a non-negative float (alpha >= 0).
kernel{"linear", "rbf", "poly", "sigmoid", "precomputed"}"linear"Kernel mapping used to construct the Gram matrix.
gammafloat | NoneNoneKernel coefficient for "rbf", "poly", and "sigmoid". If None, defaults to 1.0 / n_features.
degreeint3Degree of the polynomial kernel (kernel="poly"). Ignored by other kernels.
coef0float1.0Zero coefficient for "poly" and "sigmoid" kernels.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
dual_coef_ndarray of shape (n_samples,) or (n_samples, n_targets)Representation of the weight vector(s) in kernel space.
X_fit_ndarray of shape (n_samples, n_features)Training feature matrix stored for kernel evaluation at prediction time.
n_features_in_intNumber of features seen during fit().
device_str | NoneResolved execution device string.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> KernelRidgeFit the Kernel Ridge regression model on 1D or 2D targets y with optional sample weights.
predictpredict(X) -> ndarrayPredict target values for samples in X (1D if trained on 1D y, 2D otherwise).
scorescore(X, y, sample_weight=None) -> floatReturn the coefficient of determination (R^2) on (X, y).
toto(device) -> KernelRidgeMove the estimator's compute target to another device.
savesave(path) -> NoneSave the fitted model to a .dit binary file.
loadKernelRidge.load(path, device=None) -> KernelRidgeClass method to load a fitted model from a .dit file.

Example

import numpy as np
from ochreml.kernel_ridge import KernelRidge

X = np.linspace(-3.0, 3.0, 50).reshape(-1, 1)
y = np.cos(X[:, 0])

model = KernelRidge(alpha=0.1, kernel="rbf", gamma=0.5)
model.fit(X, y)

X_test = np.array([[-1.5], [0.0], [1.5]])
print("Predictions:", model.predict(X_test))
print("R^2 score:", model.score(X, y))
print("dual_coef_ shape:", model.dual_coef_.shape)

On this page