Link Search Menu Expand Document

PCA (Principle Component Analysis)

Principal component analysis (PCA) is the process of computing the principal components and using them to perform a change of basis on the data, sometimes using only the first few principal components and ignoring the rest.

Import

import * as Datacook from 'datacook';
const { PCA } = DataCook.Model;

Constructor

const pca = new PCA({ nComponent: 5, method: 'correlation' });

Option Parameters

parametertypedescription
nComponentnumberthe estimated number of components. If not specified, it equals the lesser value of nFeatures and nSamples
method‘covariance’|’correlation’Method used for decomposition. default=’covariance’. ‘covariance’: use covariance matrix for decomposition. ‘correlation’: use correlation matrix for decomposition.

Methods

fit

Fit PCA model with input xData

Syntax

async fit(xData: Tensor | RecursiveArray<number>): Promise<void>

Paramters

Parametertypedescription
xDataTensor | RecursiveArrayinput data of shape (nSamples,nFeatures) in type of array or tensor

transform

ransform method of PCA, transform original data to reduced features.

Syntax

async transform(xData: Tensor | RecursiveArray<number>): Promise<Tensor> 

Parameters

Parametertypedescription
xDataTensor | RecursiveArrayinput data of shape (nSamples,nFeatures) in type of array or tensor

Example

PCA of iris data

import * as DataCook from '@pipcook/datacook';
import {PCA} from DataCook.Model;

const pca = new PCA({ nComponents: 3 });
await pca.fit(irisData);
const fittedNTensors = tf.memory().numTensors;
assert.isTrue(fittedNTensors === nTensorsBase + 5);
console.log('explained variance:', pca.explainedVariance.arraySync());
console.log('explained varaince ratio:', pca.explainedVarianceRatio.arraySync());