Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

NMF

NMF factorizes a non-negative matrix X into non-negative matrices W and H whose product approximates X. It is useful for additive representations such as dimensionality reduction, source separation, and topic extraction.

How it works

NMF approximates a non-negative input matrix X as the product of two non-negative matrices W and H. It alternately minimizes the objective by updating W and H, with coordinate descent or multiplicative updates controlling the numerical optimization.

Syntax

NMF(n_components='auto', *, init=None, solver='cd', beta_loss='frobenius', tol=0.0001, max_iter=200, random_state=None, alpha_W=0.0, alpha_H='same', l1_ratio=0.0, verbose=0, shuffle=False, regularization='deprecated')

Parameters

NameTypeDescription
n_componentsint, {'auto'} or None, default='auto'Number of components; an integer, 'auto', or None.
solver{'cd', 'mu'}, default='cd'Numerical optimization method: coordinate descent or multiplicative updates.
other parameterssee signatureInitialization and optimization parameters are also accepted by the class; see the official signature for the complete list.

Returns

A fitted NMF estimator; fit_transform additionally returns the transformed matrix W.

Key points

  • The model learns non-negative factors W and H whose product reconstructs the non-negative input approximately.
  • NMF supports dimensionality reduction, separating sources, and extracting topics.
  • Optimization alternates between updating W and updating H.

Example: Fit and inspect an NMF model

import numpy as np
from sklearn.decomposition import NMF

X = np.array([[1, 1], [2, 1], [3, 1], [4, 1]], dtype=float)
model = NMF(n_components=2, init="random", random_state=0)
W = model.fit_transform(X)
H = model.components_
print(W.shape, H.shape)

Fits NMF to a non-negative matrix, returning W while exposing H through components_.

When to use it

Dimensionality reduction

Compress non-negative feature matrices into fewer additive dimensions.

Source separation

Recover additive signals from mixtures represented by non-negative measurements.

Topic extraction

Discover additive latent themes in non-negative document-term matrices.

Interview questions

QWhat does NMF compute?

NMF factorizes a non-negative matrix X into non-negative W and H such that WH approximates X. The objective is minimized by alternating updates of W and H.

Follow-up: How do you obtain the factors in scikit-learn? fit_transform returns W, while the fitted model stores H in components_.

QWhen would you use NMF instead of PCA?

NMF is appropriate when the input matrix has no negative values. It can support dimensionality reduction, source separation, and topic extraction.

Follow-up: Why can its representations be interpretable? Components are combined additively, without subtracting one component from another.

QWhich optimization and regularization choices does NMF expose?

The class supports coordinate descent through solver='cd' and multiplicative updates through solver='mu'. Initialization can be random, and the loss can include L1 and L2 regularization priors.

Follow-up: What does transform do after fitting? It decomposes a new matrix using the stored components.

  • PCA
  • TruncatedSVD
  • DictionaryLearning
  • MiniBatchNMF

Source: NMF. Checked 2026-10-15.