Table of Contents

Class KernelPCA<T>

Namespace
AiDotNet.Preprocessing.DimensionalityReduction
Assembly
AiDotNet.dll

Kernel Principal Component Analysis for non-linear dimensionality reduction.

public class KernelPCA<T> : TransformerBase<T, Matrix<T>, Matrix<T>>, IDataTransformer<T, Matrix<T>, Matrix<T>>

Type Parameters

T

The numeric type for calculations (e.g., float, double).

Inheritance
TransformerBase<T, Matrix<T>, Matrix<T>>
KernelPCA<T>
Implements
IDataTransformer<T, Matrix<T>, Matrix<T>>
Inherited Members

Remarks

Kernel PCA is an extension of PCA that uses a kernel function to map data into a higher-dimensional feature space where non-linear relationships become linear, then performs standard PCA in that space.

This allows capturing non-linear relationships that standard PCA cannot. Common kernels include RBF (Gaussian), polynomial, and sigmoid.

For Beginners: Regular PCA finds straight-line patterns. Kernel PCA can find curved patterns by mathematically "bending" the data: - RBF kernel: Good for data with clusters or blobs - Polynomial: Good for polynomial relationships - Linear: Same as regular PCA

Think of it as finding principal components in a transformed space.

Constructors

KernelPCA(int, KernelType, double, double, double, bool, double, int[]?)

Creates a new instance of KernelPCA<T>.

public KernelPCA(int nComponents = 2, KernelType kernel = KernelType.RBF, double gamma = 1, double degree = 3, double coef0 = 1, bool fitInverseTransform = false, double alpha = 0.001, int[]? columnIndices = null)

Parameters

nComponents int

Number of components to keep. Defaults to 2.

kernel KernelType

The kernel function to use. Defaults to RBF.

gamma double

Kernel coefficient for RBF/poly/sigmoid. Defaults to 1.0.

degree double

Polynomial degree. Defaults to 3.

coef0 double

Independent term in polynomial/sigmoid. Defaults to 1.0.

fitInverseTransform bool

Whether to learn inverse transformation. Defaults to false.

alpha double

Regularization for inverse transform. Defaults to 1e-3.

columnIndices int[]

The column indices to use, or null for all columns.

Properties

Gamma

Gets the gamma parameter for RBF and polynomial kernels.

public double Gamma { get; }

Property Value

double

Kernel

Gets the kernel type.

public KernelType Kernel { get; }

Property Value

KernelType

Lambdas

Gets the eigenvalues.

public double[]? Lambdas { get; }

Property Value

double[]

NComponents

Gets the number of components.

public int NComponents { get; }

Property Value

int

SupportsInverseTransform

Gets whether this transformer supports inverse transformation.

public override bool SupportsInverseTransform { get; }

Property Value

bool

Methods

FitCore(Matrix<T>)

Fits KernelPCA by computing the kernel matrix and its eigenvectors.

protected override void FitCore(Matrix<T> data)

Parameters

data Matrix<T>

The training data matrix.

GetFeatureNamesOut(string[]?)

Gets the output feature names after transformation.

public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)

Parameters

inputFeatureNames string[]

Returns

string[]

InverseTransformCore(Matrix<T>)

Transforms data back to approximate original space.

protected override Matrix<T> InverseTransformCore(Matrix<T> data)

Parameters

data Matrix<T>

The transformed data.

Returns

Matrix<T>

Approximate reconstruction in original space.

TransformCore(Matrix<T>)

Transforms the data using kernel PCA.

protected override Matrix<T> TransformCore(Matrix<T> data)

Parameters

data Matrix<T>

The data to transform.

Returns

Matrix<T>

The transformed data.