@kanaries/ml
API Reference/Decomposition

Incremental PCA

Process numeric data in batches with the @kanaries/ml IncrementalPCA JavaScript implementation for memory-aware browser and Node.js workflows.

View as Markdown

Algorithm overview

Incremental PCA updates a low-rank SVD one batch at a time. It is useful when a complete dataset should not be retained in memory, while still producing PCA-style projections and inverse transforms.

JavaScript implementation

@kanaries/ml follows sklearn's mean-corrected incremental SVD update. Call partialFit for streamed batches, or fit to let the estimator split an in-memory matrix by batchSize.

Quick start example

import { Decomposition } from '@kanaries/ml';

const batchOne = [[0, 1, 2], [1, 2, 3], [2, 1, 4]];
const batchTwo = [[3, 4, 2], [4, 3, 1], [5, 5, 0]];
const nextBatch = [[2, 3, 2]];
const pca = new Decomposition.IncrementalPCA({ nComponents: 2 });
pca.partialFit(batchOne).partialFit(batchTwo);
const embedding = pca.transform(nextBatch);
console.log(embedding);

Detailed API reference

new Decomposition.IncrementalPCA({ nComponents?: number | null, batchSize?: number })
  • partialFit(X: number[][]): this updates the retained SVD.
  • fit(X: number[][]): void resets and processes batches.
  • transform and inverseTransform project data.
  • components, mean, explainedVariance, singularValues, and nSamplesSeen expose learned state.

The first partialFit batch must contain at least nComponents samples and features.