Skip to main content

Overview

NMF (Non-negative Matrix Factorization) decomposes non-negative data into non-negative components, making it ideal for parts-based representations where negative values don’t make sense (e.g., images, text).

Constructor

new NMF(options?: NMFOptions)
options
NMFOptions
Configuration options for NMF

Methods

fit

fit(X: Matrix): this
Learn NMF components from non-negative training data.
X
Matrix
required
Non-negative training data of shape [nSamples, nFeatures]
Returns: The fitted NMF instance

transform

transform(X: Matrix): Matrix
Transform data to component representation.
X
Matrix
required
Non-negative 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
Non-negative training data of shape [nSamples, nFeatures]
Returns: Transformed data of shape [nSamples, nComponents]

inverseTransform

inverseTransform(W: Matrix): Matrix
Reconstruct data from component representation.
W
Matrix
required
Component weights of shape [nSamples, nComponents]
Returns: Reconstructed data of shape [nSamples, nFeatures]

Properties

components_
Matrix | null
Non-negative components (basis vectors), shape [nComponents, nFeatures]
reconstructionErr_
number | null
Frobenius norm of the reconstruction error
nIter_
number | null
Number of iterations run for convergence
nFeaturesIn_
number | null
Number of features seen during fit
nComponents_
number | null
Number of components extracted

Example

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

// Non-negative data (e.g., image patches, word counts)
const data = [
  [1, 2, 0, 3],
  [4, 0, 5, 1],
  [0, 3, 2, 4],
  [2, 1, 3, 0],
  [3, 4, 1, 2],
];

const nmf = new NMF({ 
  nComponents: 2,
  randomState: 42,
  maxIter: 500 
});

const transformed = nmf.fitTransform(data);
console.log('Component weights:', transformed);
console.log('Basis components:', nmf.components_);
console.log('Reconstruction error:', nmf.reconstructionErr_);
console.log('Converged in', nmf.nIter_, 'iterations');

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

Build docs developers (and LLMs) love