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 )
Configuration options for NMF Number of components to extract.
Maximum number of iterations for multiplicative update.
Convergence tolerance for stopping criterion.
Seed for random initialization for reproducible results.
Methods
fit
Learn NMF components from non-negative training data.
Non-negative training data of shape [nSamples, nFeatures]
Returns: The fitted NMF instance
transform ( X : Matrix ): Matrix
Transform data to component representation.
Non-negative 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.
Non-negative training data of shape [nSamples, nFeatures]
Returns: Transformed data of shape [nSamples, nComponents]
inverseTransform ( W : Matrix ): Matrix
Reconstruct data from component representation.
Component weights of shape [nSamples, nComponents]
Returns: Reconstructed data of shape [nSamples, nFeatures]
Properties
Non-negative components (basis vectors), shape [nComponents, nFeatures]
Frobenius norm of the reconstruction error
Number of iterations run for convergence
Number of features seen during fit
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 );