Overview
KernelPCA extends PCA to capture non-linear relationships in data by implicitly mapping it to a high-dimensional feature space using kernel functions.
Constructor
new KernelPCA(options?: KernelPCAOptions)
Configuration options for KernelPCA
Number of components to keep. If not set, all components are kept.
kernel
'linear' | 'rbf' | 'poly'
default:"'rbf'"
Kernel function to use:
'linear': Linear kernel (equivalent to standard PCA)
'rbf': Radial Basis Function kernel (Gaussian)
'poly': Polynomial kernel
Kernel coefficient for ‘rbf’ and ‘poly’. Defaults to 1/nFeatures.
Degree for polynomial kernel (ignored for other kernels).
Independent term in polynomial kernel.
Convergence tolerance for eigenvalue decomposition.
Maximum iterations for eigenvalue decomposition.
Methods
fit
Compute kernel principal components from training data.
Training data of shape [nSamples, nFeatures]
Returns: The fitted KernelPCA instance
transform(X: Matrix): Matrix
Project data onto kernel principal components.
Data to transform of shape [nSamples, nFeatures]
Returns: Transformed data of shape [nSamples, nComponents]
fitTransform(X: Matrix): Matrix
Fit the model and transform data in one step.
Training data of shape [nSamples, nFeatures]
Returns: Transformed data of shape [nSamples, nComponents]
Properties
Eigenvectors in kernel space, shape [nSamples, nComponents]
Eigenvalues of the centered kernel matrix
Number of features seen during fit
Example
import { KernelPCA } from '@elucidate/elucidate';
// Non-linear data (e.g., circular pattern)
const data = [
[1.0, 0.0],
[0.9, 0.4],
[0.5, 0.9],
[0.0, 1.0],
[-0.5, 0.9],
[-0.9, 0.4],
[-1.0, 0.0],
[-0.9, -0.4],
];
// RBF kernel for circular patterns
const kpca = new KernelPCA({
nComponents: 2,
kernel: 'rbf',
gamma: 1.0
});
const transformed = kpca.fitTransform(data);
console.log('Transformed:', transformed);
console.log('Eigenvalues:', kpca.lambdas_);
// Polynomial kernel for polynomial relationships
const kpcaPoly = new KernelPCA({
nComponents: 2,
kernel: 'poly',
degree: 3,
gamma: 1.0,
coef0: 1
});
const polyTransformed = kpcaPoly.fitTransform(data);
console.log('Polynomial transformed:', polyTransformed);