How this calculation works
Approximate a nonnegative matrix with a chosen number of additive components, checking reconstruction and projected-gradient status.
min_(W≥0,H≥0) ½||X−WH||F²; W is m×r and H is r×n. Each scalar coordinate is minimized with the other factor fixed.A worked example
X=[[2,1,0],[0,1,2],[2,2,2]] has exact factors W=[[1,0],[0,1],[1,1]], H=[[2,1,0],[0,1,2]]. The chosen rank2 can reconstruct X exactly; the numerical multistart fit returns residual approximately3.63×10^-8.
Good to know
1–12rows/columns; nonnegative entries≤10^6; chosen rank1–min(4,m,n). Four deterministic starts;10–3000 coordinate sweeps/start. Local fit, capped states explicit, no global optimality, uniqueness or nonnegative-rank certificate.
Reference: Scikit-learn NMF (opens in a new tab) ↗
Nonnegative factors describe additive components
NMF approximates a nonnegative data matrix by W times H, with both factors constrained to be nonnegative. Each rank component contributes an outer product that can add to an observation but cannot cancel another component through a negative weight.
Rows and columns must represent comparable measurements under a chosen scaling. The constraints can be useful for component interpretation, but a numerically valid factorization does not prove that each component corresponds to a real physical source.
Compare this with the Nonnegative least-squares calculator. Nonnegative least squares solves coefficients against a fixed design, not both factors simultaneously.
Choose rank independently from the solver budget
The rank input fixes the number of columns of W and rows of H. A larger rank generally allows a smaller residual, but also increases flexibility and can make components less stable or less interpretable.
The sweep limit and tolerance control numerical fitting, not statistical model selection. This page tries four deterministic positive initializations. Changing rank is a different question from allowing an existing fit more iterations.
Coordinate minimization maintains nonnegativity
For fixed H, the objective is a quadratic function of each entry of W. Its coordinate minimizer can be projected onto the nonnegative half-line. Alternating these updates with corresponding H updates gives nonincreasing residual objectives up to roundoff.
The projected gradient checks first-order stationarity under the nonnegative constraints. A small gradient is stronger evidence than a small change in the objective alone, but the joint problem is nonconvex and does not supply a global-optimum certificate.
Inspect the prefilled rank-two reconstruction
The third row of the displayed matrix is the sum of its first two rows. Taking the first two rows as H and W rows(1,0),(0,1),(1,1) therefore gives an exact nonnegative rank-two reconstruction.
The numerical fit should have a residual close to zero, even though its W and H need not match those particular factors. This is a useful invariant: check their product rather than demanding identical component labels or scales.
Factor scaling and order are ambiguous
Multiplying one W column by a positive constant and dividing the corresponding H row by the same constant leaves the product unchanged. Permuting matched components has the same effect on reconstruction.
Consequently, comparing two runs requires component alignment and normalization. A low reconstruction residual does not remove this ambiguity, and an individual factor entry should not be interpreted as a uniquely identified parameter.
Read cap and stationarity status honestly
The output retains the status of each start, its objective history and projected-gradient diagnostic. A bounded iteration result may be useful as an approximation, but it must be read with its convergence status rather than relabeled a certified optimum.
Zero input is handled explicitly with zero factors. Negative entries are rejected because they violate the model. Sparse zeros can also make component behavior different from a small positive perturbation, so preprocessing choices belong in the interpretation.
Use residuals to compare a controlled experiment
Compare candidate ranks using the same data transformation and a relevant held-out or domain assessment. A smaller training residual by itself rewards flexibility and is not evidence that a richer factorization generalizes better.
The objective here is unregularized Frobenius loss. There is no Poisson likelihood, missing-value mask, sparsity penalty, topic-model posterior or uncertainty interval hidden behind the result. Those changes require different objectives or validation procedures.
A few questions, answered
Is the global optimum certified?
No. Multistart coordinate fitting reports local numerical diagnostics.
Are the factors unique?
No. Positive rescaling and matched component permutations preserve their product.
Can I use negative data?
No. NMF requires nonnegative input and factor entries.
Does a larger rank always mean a better model?
A smaller training residual alone does not establish usefulness or generalization.