Skip to main content

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)
options
KernelPCAOptions
Configuration options for KernelPCA

Methods

fit

fit(X: Matrix): this
Compute kernel principal components from training data.
X
Matrix
required
Training data of shape [nSamples, nFeatures]
Returns: The fitted KernelPCA instance

transform

transform(X: Matrix): Matrix
Project data onto kernel principal components.
X
Matrix
required
Data to transform of shape [nSamples, nFeatures]
Returns: Transformed data of shape [nSamples, nComponents]

fitTransform

fitTransform(X: Matrix): Matrix
Fit the model and transform data in one step.
X
Matrix
required
Training data of shape [nSamples, nFeatures]
Returns: Transformed data of shape [nSamples, nComponents]

Properties

alphas_
Matrix | null
Eigenvectors in kernel space, shape [nSamples, nComponents]
lambdas_
Vector | null
Eigenvalues of the centered kernel matrix
nFeaturesIn_
number | null
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);

Build docs developers (and LLMs) love