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 )
Configuration options for PCA Number of components to keep. If not set, all components are kept.
When true, components are divided by the square root of explained variance to ensure unit component-wise variance.
Convergence tolerance for eigenvalue decomposition.
Maximum iterations for eigenvalue decomposition.
Methods
fit
Compute principal components from training data.
Training data of shape [nSamples, nFeatures]
Returns: The fitted PCA instance
transform ( X : Matrix ): Matrix
Project data onto 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]
inverseTransform ( X : Matrix ): Matrix
Reconstruct original data from transformed components.
Transformed data of shape [nSamples, nComponents]
Returns: Reconstructed data of shape [nSamples, nFeatures]
Properties
Principal axes in feature space, shape [nComponents, nFeatures]
Amount of variance explained by each component
Percentage of variance explained by each component
Singular values corresponding to each component
Per-feature mean of training data
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 );