Non-negative matrix factorization

Also known as: NMF · NNMF

On this page 7

Non-negative matrix factorization (NMF, also NNMF) is an unsupervised machine learning method that approximates a non-negative data matrix V as the product of two smaller non-negative matrices, W and H, so that V is approximately W times H. Because no entry may be negative, every data point is reconstructed as an additive combination of parts, which makes the learned components unusually easy to interpret.

NMF became widely known after Lee and Seung showed in 1999 that it learns parts-based representations of faces and text. In machine learning it is used for dimensionality reduction, topic modeling, audio source separation, recommender systems and the analysis of gene expression, spectra and other measurements that are naturally non-negative.

How NMF decomposes a data matrix

Given a matrix V of size m by n, for example m words by n documents, NMF chooses a rank k much smaller than m and n and finds W of size m by k and H of size k by n. Each column of W is a basis component, such as a topic or a spectral pattern, and each column of H holds the non-negative weights that say how much of each component a given sample contains.

The factors are found by minimizing a reconstruction error between V and WH subject to the non-negativity constraints. The problem is non-convex in W and H jointly, so algorithms find a local minimum and the result depends on initialization. Unlike singular value decomposition, NMF has no closed-form solution and its components are not ordered by importance or mutually orthogonal.

The table below describes the objects involved in a factorization.

ElementShapeMeaningConstraint
V (data matrix)m by nObserved non-negative dataAll entries zero or positive
W (basis matrix)m by kComponents, parts or topicsNon-negative
H (coefficient matrix)k by nActivation of each component per sampleNon-negative
k (rank)ScalarNumber of componentsUsually much smaller than m and n
Elements of an NMF model

Objective functions and algorithms

The choice of loss function should match the noise model of the data. The Frobenius norm assumes Gaussian noise and suits continuous measurements, the generalized Kullback-Leibler divergence corresponds to Poisson counts such as word frequencies, and the Itakura-Saito divergence is scale-invariant and popular for audio power spectrograms. All three belong to the broader beta-divergence family.

LossImplied noise modelTypical data
Frobenius normGaussianImages, continuous signals
Kullback-Leibler divergencePoissonWord counts, event counts
Itakura-Saito divergenceMultiplicative gammaAudio power spectra
Common NMF loss functions

Classic multiplicative update rules rescale each entry of W and H by a ratio of positive terms, which keeps factors non-negative automatically and is simple to implement, but convergence can be slow. Alternating non-negative least squares and coordinate descent usually converge faster and are the default in modern libraries. Initialization with NNDSVD, a non-negative variant of singular value decomposition, gives more stable and reproducible results than random starting points.

Computational cost per iteration is roughly proportional to m times n times k for dense data and to the number of non-zero entries times k for sparse data. Large corpora are therefore handled with sparse matrix storage, mini-batch or online NMF variants that update factors from chunks of samples, and GPU implementations. A common stopping rule halts training once the relative change in loss falls below about 0.0001, although a fixed iteration budget is also widely used.

NMF is often contrasted with principal component analysis and other matrix factorizations because all of them compress data into a small number of latent factors. The comparison below shows where NMF differs in practice.

MethodSign constraintsComponent structureInterpretability
NMFNon-negative factorsAdditive, often sparse partsHigh for non-negative data
PCA / SVDNoneOrthogonal, ordered by varianceLower, components mix signs
Independent component analysisNoneStatistically independent sourcesModerate
Latent Dirichlet allocationProbabilistic, non-negativeTopic distributionsHigh, with uncertainty estimates
k-means clusteringHard assignmentsOne centroid per sampleHigh but coarse
NMF and related factorization methods

With KL divergence, NMF is mathematically closely related to probabilistic latent semantic analysis, and with orthogonality constraints on H it behaves like a soft form of k-means clustering. These links explain why NMF topics and clusters often resemble those of probabilistic models.

Choosing the rank and preparing data

The rank k is the most consequential hyperparameter. A low rank merges distinct patterns, whereas a high rank splits them into redundant or noisy components. Practitioners compare reconstruction error curves, cross-validated error on masked entries, topic coherence for text and the stability of components across random restarts.

Preprocessing matters as much as the algorithm. Text is usually weighted with TF-IDF and normalized, spectra are kept on a magnitude or power scale, and very sparse rows or columns are removed. Regularization with L1 penalties produces sparser components, while L2 penalties stabilize solutions on noisy data.

Data typeRecommended lossTypical rankPreprocessing
Document-term matrixFrobenius on TF-IDF or KL on counts10-100Stop word removal, normalization
Audio spectrogramItakura-Saito or KL5-50Magnitude or power scale
Gene expressionFrobenius or KL2-20Filtering low-variance genes
User-item ratingsFrobenius on observed entries10-200Masking missing values
Practical choices by data type

Limitations and common mistakes

  • Non-uniqueness: different runs can yield different factorizations with similar error, so components should be checked for stability.
  • Scale ambiguity: W can be multiplied and H divided by the same diagonal matrix, so normalize columns before comparing components.
  • Negative inputs: centered or standardized data violate the model; shift or transform the data instead of clipping values.
  • Treating zeros as missing: in recommender data, unobserved entries must be masked rather than fitted as true zeros.
  • Over-reading components: interpretable parts are a tendency, not a guarantee, and require domain validation.

The idea in brief

Non-negative matrix factorization takes a large table of positive numbers and explains it with a few building blocks. Think of a smoothie. NMF guesses the ingredients and how much of each went in.

Because nothing can be subtracted, every item is a sum of parts. That is why the parts often make sense to people. A pile of articles becomes a short list of themes, and each article becomes a mix of those themes.

A practical scenario

A customer support team at a software company receives about 40,000 tickets a month and wants to know what customers complain about without reading every message. An analyst builds a TF-IDF document-term matrix with 15,000 terms and runs NMF with NNDSVD initialization, testing ranks from 10 to 40.

Topic coherence and stability across restarts peak at rank 22. The components map cleanly to themes such as login failures, invoice errors and slow exports, and the H matrix gives each ticket a weighted theme profile. Monthly tracking of these weights reveals that export complaints doubled after a release, which leads engineers to a performance regression that is fixed in the next update.