Language: English
Last updated: 2026-07-24
Switch: Chinese
The kernel-methods module provides:
KernelRidgeKernelRidgeCVKernelPCANystroempairwise_kernels- RBF, polynomial, linear, Laplacian, sigmoid, cosine, and chi-squared kernels
The public implementations expose NumPy, CuPy, and Torch execution paths where supported by the selected estimator and kernel.
statgpu.nonparametric.kernel_methods.KernelRidge
statgpu.nonparametric.kernel_methods.KernelRidgeCV
statgpu.nonparametric.kernel_methods.KernelPCA
statgpu.nonparametric.kernel_methods.Nystroem
statgpu.nonparametric.kernel_methods.pairwise_kernels
Individual kernel functions are also importable from
statgpu.nonparametric.kernel_methods.
Given a training kernel matrix
Equivalently, the dual objective is
Predictions for test observations are
KernelRidge solves the regularized linear system directly. Multi-output
responses are supported when the fitted implementation receives a compatible
response matrix.
KernelRidgeCV evaluates a grid of regularization parameters across CV folds and
refits the selected value on the complete dataset. Backend-specific
implementations may reuse a kernel eigendecomposition or vectorize the alpha
sweep rather than solving every system independently.
The selected alpha is exposed as alpha_. CV diagnostics are stored in
cv_results_; consult the fitted object for the exact fields produced by the
selected path.
For the centered kernel matrix
The leading eigenvectors define nonlinear components. Transforming new data requires computing the test-to-training kernel, applying the training centering quantities, and projecting onto the retained components.
Nystroem selects
then the transformed features have the form
This replaces a full
| Kernel | Definition |
|---|---|
| RBF | |
| Polynomial | |
| Linear | |
| Laplacian | |
| Sigmoid | |
| Cosine | |
| Chi-squared |
The chi-squared kernel requires non-negative input features. A callable kernel may be supplied where accepted by the estimator; it must return an array on the requested backend and obey the expected pairwise-kernel shape.
| Parameter | Default | Description |
|---|---|---|
alpha |
1.0 |
Ridge regularization strength |
kernel |
"rbf" |
Built-in kernel name or callable |
gamma |
None |
Kernel coefficient where applicable |
degree |
3 |
Polynomial degree |
coef0 |
1 |
Polynomial or sigmoid intercept term |
kernel_params |
None |
Additional callable-kernel parameters |
device |
"auto" |
"cpu", "cuda" (CuPy), "torch", or "auto" |
n_jobs |
None |
Reserved where parallel execution is not implemented |
In addition to the kernel parameters:
| Parameter | Default | Description |
|---|---|---|
alphas |
None |
Candidate regularization strengths |
cv |
5 |
Number of CV folds |
random_state |
None |
Fold random state where used |
Common parameters include n_components, kernel, gamma, degree, coef0,
alpha, eigen_solver, and device.
Common parameters include kernel, n_components, gamma, degree, coef0,
random_state, and device.
Consult class docstrings for exact aliases, accepted callables, and parameter validation.
Typical fitted state includes the training observations, dual coefficients,
resolved kernel parameters, and fitted feature counts. predict(X) returns the
same backend family as the maintained estimator path. score(X, y) reports the
coefficient of determination.
In addition to the final refitted state, the model exposes alpha_,
best_score_ where implemented, and cv_results_.
Fitted outputs include retained eigenvalues/eigenvectors or normalized dual
components, training-kernel centering quantities, and transformed components
from fit_transform or transform.
Fitted state includes landmark indices or components and the normalization matrix.
transform(X) returns the explicit approximate feature map.
import numpy as np
from statgpu.nonparametric.kernel_methods import (
KernelRidge,
KernelRidgeCV,
KernelPCA,
Nystroem,
)
rng = np.random.default_rng(42)
X = rng.normal(size=(500, 10))
y = X[:, 0] - 0.5 * X[:, 1] + rng.normal(scale=0.1, size=500)
kr = KernelRidge(alpha=1.0, kernel="rbf", device="cpu").fit(X, y)
print(kr.score(X, y))
kr_cv = KernelRidgeCV(kernel="rbf", cv=5, device="cpu").fit(X, y)
print(kr_cv.alpha_)
kpca = KernelPCA(n_components=3, kernel="rbf", device="cpu")
X_kpca = kpca.fit_transform(X)
nystroem = Nystroem(kernel="rbf", n_components=50, random_state=42)
X_features = nystroem.fit_transform(X)import cupy as cp
from statgpu.nonparametric.kernel_methods import KernelRidgeCV
X = cp.random.randn(500, 10, dtype=cp.float64)
y = X[:, 0] - 0.5 * X[:, 1]
model = KernelRidgeCV(kernel="rbf", cv=5, device="cuda").fit(X, y)import torch
from statgpu.nonparametric.kernel_methods import KernelRidgeCV
X = torch.randn(500, 10, device="cuda", dtype=torch.float64)
y = X[:, 0] - 0.5 * X[:, 1]
model = KernelRidgeCV(kernel="rbf", cv=5, device="torch").fit(X, y)device="cuda" selects CuPy; device="torch" selects Torch.
Pairwise kernel construction, regularized solves, eigendecompositions, projections, and transformed feature arrays remain on the selected backend where supported. Small random-index metadata, CV fold indices, parameter bookkeeping, and scalar scores may be represented on CPU. Explicit device requests do not silently select another backend.
KernelPCA and Nystroem reject NaN/Inf during fitting and transformation on
maintained validation paths. Kernel-specific domain checks, such as non-negative
inputs for the chi-squared kernel, fail explicitly.
Kernel methods do not currently expose coefficient-level standard errors, hypothesis tests, or confidence intervals. Model quality is evaluated through prediction scores, cross-validation loss, embedding properties, reconstruction or approximation diagnostics, and application-specific validation.
There is no strict/approximate inference mode distinction in this module. Nystroem is an explicit low-rank kernel approximation, not a silent fallback for an exact kernel estimator.
- Exact kernel methods materialize an
$n\times n$ training kernel and therefore have quadratic memory cost. - Direct kernel-ridge solves and dense eigendecompositions have cubic worst-case arithmetic cost in the number of training observations.
-
KernelRidgeCVmay reuse decompositions across alpha values, but CV still multiplies work across folds. -
Nystroemreduces kernel storage to$O(nm)$ plus landmark linear algebra. - GPU speed depends on sample size, dtype, kernel, synchronization, and available memory; small problems may be faster on CPU.
- Kernel matrices can become poorly conditioned; increase
alphaor adjust the kernel scale when necessary. - RBF-like kernels are sensitive to
gamma. - Chi-squared kernels require non-negative inputs.
- Dense exact kernel methods may exhaust device memory for large
$n$ . - User-supplied kernels are responsible for backend, dtype, shape, and symmetry contracts required by the selected estimator.
-
KernelRidgeCVcan be expensive for large fold and alpha grids.
Maintained tests cover NumPy/Torch parity, finite-input validation, rank-deficient kernel safeguards, CV alpha selection and refit behavior, kernel-domain errors, and output-backend preservation. Accuracy and performance claims remain scoped to the exact estimator, backend, hardware, and commit recorded by the corresponding test or benchmark artifact.
Use exact Kernel Ridge when the training kernel fits comfortably in memory and the exact kernel representation is important. Use Nystroem when a controlled low-rank feature approximation is preferable for scale or downstream reuse.
Kernel construction and linear algebra must be large enough to amortize device launch, synchronization, and memory-transfer overhead.
No. "auto" is itself an automatic selection request. An explicit "cuda" or
"torch" request fails if that backend is unavailable.
Eigenvector signs and bases within repeated or nearly repeated eigenspaces are not uniquely identified. Compare represented subspaces or downstream quantities when that ambiguity matters.
- Schölkopf, B., Smola, A., & Müller, K.-R. (1998). Nonlinear component analysis as a kernel eigenvalue problem.
- Williams, C. K. I., & Seeger, M. (2001). Using the Nystroem method to speed up kernel machines.
- Shawe-Taylor, J., & Cristianini, N. (2004). Kernel Methods for Pattern Analysis.