Skip to Content
OchreML
Decomposition & Manifold

KernelPCA

Nonlinear dimensionality reduction using kernel Gram matrices and double-centered eigendecomposition.

KernelPCA performs nonlinear dimensionality reduction by mapping samples implicitly into a reproducing kernel Hilbert space (RKHS) and computing the eigendecomposition of the double-centered kernel Gram matrix K_tilde. It supports linear, polynomial, RBF, sigmoid, and precomputed kernels, as well as optional ridge-based inverse transforms. (^▽^)

Backend Support

  • Backend primitives: E1 (dense matrix multiplication), N3 (pairwise distance / kernel Gram matrix construction), N5 (symmetric eigendecomposition).
  • Supported devices: "auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0". Falls back cleanly to multi-threaded Rayon CPU (f64) when an accelerator is unavailable.

Import & Signature

from ochreml.decomposition import KernelPCA
# Or via top-level lazy import:
from ochreml import KernelPCA
KernelPCA(
    n_components: Optional[int] = None,
    kernel: str = "linear",
    gamma: Optional[float] = None,
    degree: int = 3,
    coef0: float = 1.0,
    fit_inverse_transform: bool = False,
    alpha: float = 1.0,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_componentsint or NoneNoneNumber of components to keep. If None, all non-zero components are retained.
kernel{"linear", "poly", "rbf", "sigmoid", "precomputed"}"linear"Kernel function used to construct the Gram matrix.
gammafloat or NoneNoneKernel coefficient for "rbf", "poly", and "sigmoid". Defaults to 1.0 / n_features when None.
degreeint3Polynomial degree for kernel="poly". Ignored by other kernels.
coef0float1.0Independent additive term in "poly" and "sigmoid" kernels.
fit_inverse_transformboolFalseIf True, learns an approximate inverse mapping from embedded space back to input space using kernel ridge regression (not supported for kernel="precomputed").
alphafloat1.0Regularization hyperparameter for the ridge regression used when fit_inverse_transform=True.
devicestr or NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeType / ShapeDescription
eigenvalues_ndarray of shape (n_components,)Eigenvalues of the centered kernel matrix in decreasing order.
eigenvectors_ndarray of shape (n_samples, n_components)Eigenvectors of the centered kernel matrix.
n_features_in_intNumber of features seen during fit.
device_strActive resolved compute device string.

Methods

MethodSignatureDescription
fitfit(X) -> KernelPCAFit the kernel PCA model on training data X.
transformtransform(X) -> ndarrayProject X into the learned kernel principal component space.
fit_transformfit_transform(X) -> ndarrayFit the model on X and return the projected coordinates.
inverse_transforminverse_transform(X) -> ndarrayReconstruct samples back to input space (requires fit_inverse_transform=True).
toto(device) -> KernelPCAMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model to a binary .dit file.
loadKernelPCA.load(path, device=None) -> KernelPCALoad a saved model from a .dit file (also supported via ochreml.load).

Example

import numpy as np
import ochreml
from ochreml.decomposition import KernelPCA

rng = np.random.default_rng(42)
theta = rng.uniform(0, 2 * np.pi, size=120)
radii = np.concatenate([np.ones(60), np.full(60, 3.0)]) + rng.normal(0, 0.1, size=120)
X = np.column_stack([radii * np.cos(theta), radii * np.sin(theta)])

kpca = KernelPCA(
    n_components=2,
    kernel="rbf",
    gamma=0.5,
    fit_inverse_transform=True,
    alpha=0.1,
    device="auto",
)
X_proj = kpca.fit_transform(X)
X_recon = kpca.inverse_transform(X_proj)

print("Projected shape:", X_proj.shape)
print("Top eigenvalues:", np.round(kpca.eigenvalues_, 4))
print("Reconstruction shape:", X_recon.shape)

kpca.save("kpca_model.dit")
loaded = ochreml.load("kpca_model.dit", device="cpu")
assert np.allclose(X_proj, loaded.transform(X))

On this page