Skip to main content

Overview

PCA (Principal Component Analysis) performs linear dimensionality reduction using eigendecomposition of the covariance matrix. It projects data onto orthogonal axes that capture maximum variance.

Constructor

new PCA(options?: PCAOptions)
options
PCAOptions
Configuration options for PCA

Methods

fit

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

transform

transform(X: Matrix): Matrix
Project data onto 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]

inverseTransform

inverseTransform(X: Matrix): Matrix
Reconstruct original data from transformed components.
X
Matrix
required
Transformed data of shape [nSamples, nComponents]
Returns: Reconstructed data of shape [nSamples, nFeatures]

Properties

components_
Matrix | null
Principal axes in feature space, shape [nComponents, nFeatures]
explainedVariance_
Vector | null
Amount of variance explained by each component
explainedVarianceRatio_
Vector | null
Percentage of variance explained by each component
singularValues_
Vector | null
Singular values corresponding to each component
mean_
Vector | null
Per-feature mean of training data
nFeaturesIn_
number | null
Number of features seen during fit

Example

import { PCA } from '@elucidate/elucidate';

const data = [
  [2.5, 2.4],
  [0.5, 0.7],
  [2.2, 2.9],
  [1.9, 2.2],
  [3.1, 3.0],
];

const pca = new PCA({ nComponents: 1 });
pca.fit(data);

const transformed = pca.transform(data);
console.log('Transformed:', transformed);
console.log('Explained variance ratio:', pca.explainedVarianceRatio_);

// Reconstruct original data
const reconstructed = pca.inverseTransform(transformed);
console.log('Reconstructed:', reconstructed);

Build docs developers (and LLMs) love