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
TThe numeric type for calculations (e.g., float, double).
- Inheritance
-
KernelPCA<T>
- Implements
- 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
nComponentsintNumber of components to keep. Defaults to 2.
kernelKernelTypeThe kernel function to use. Defaults to RBF.
gammadoubleKernel coefficient for RBF/poly/sigmoid. Defaults to 1.0.
degreedoublePolynomial degree. Defaults to 3.
coef0doubleIndependent term in polynomial/sigmoid. Defaults to 1.0.
fitInverseTransformboolWhether to learn inverse transformation. Defaults to false.
alphadoubleRegularization for inverse transform. Defaults to 1e-3.
columnIndicesint[]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
Kernel
Gets the kernel type.
public KernelType Kernel { get; }
Property Value
Lambdas
Gets the eigenvalues.
public double[]? Lambdas { get; }
Property Value
- double[]
NComponents
Gets the number of components.
public int NComponents { get; }
Property Value
SupportsInverseTransform
Gets whether this transformer supports inverse transformation.
public override bool SupportsInverseTransform { get; }
Property Value
Methods
FitCore(Matrix<T>)
Fits KernelPCA by computing the kernel matrix and its eigenvectors.
protected override void FitCore(Matrix<T> data)
Parameters
dataMatrix<T>The training data matrix.
GetFeatureNamesOut(string[]?)
Gets the output feature names after transformation.
public override string[] GetFeatureNamesOut(string[]? inputFeatureNames = null)
Parameters
inputFeatureNamesstring[]
Returns
- string[]
InverseTransformCore(Matrix<T>)
Transforms data back to approximate original space.
protected override Matrix<T> InverseTransformCore(Matrix<T> data)
Parameters
dataMatrix<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
dataMatrix<T>The data to transform.
Returns
- Matrix<T>
The transformed data.