What is Non-Negative Matrix Factorization (NMF)?

Salman Ibne Eunus
CodeX
Published in
3 min readMar 19, 2022

Non-negative Matrix Factorization or NMF is a method used to factorize a non-negative matrix, X, into the product of two lower rank matrices, A and B, such that AB approximates an optimal solution of X. This is an unsupervised learning algorithm used to reduce the dimensionality of data into lower-dimensional spaces. This technique is mostly used in recommendation systems, text mining, and image analysis applications. NMF is a state-of-the-art feature extraction algorithm that is useful when there are lots of features that are ambiguous and has weak predictability power. It can produce meaningful patterns, topics, and themes.

Retrieved from istockphoto.com

NMF uses methods that are derived from multivariate analysis and linear algebra. The algorithm iteratively changes the values of A and B such that their product approaches X. This method keeps the structure of the original data intact and makes sure that both the basis and weights are non-negative. NMF stops when the approximation error converges or a certain number of iterations has reached. It must be initialized with a seed to refer to the starting point for the iterations. This is due to the high dimensionality of the processing space and the fact that there is no global minimization algorithm. Therefore, the appropriate initialization can be critical in getting meaningful outputs. Non-negative Matrix Factorization (NMF) is often used as a preprocessing step for dimensionality reduction in tasks like — classification, clustering, regression, etc. Actually, it can be applied in any situation where the input data matrix has no negative elements.

Singular Valued Decomposition is another alternative method of matrix factorization for finding low-rank approximations. NMF is better as it shows representations in small steps and produces sparse outcomes which is much easier to understand and use than SVD.

Let’s see a code example of NMF using scikit-learn. This code cell shows the basic steps to implement NMF in a toy dataset

>>> import numpy as np
>>> X = np.array([[1, 2], [2, 2], [3, 1.2], [4, 1], [5, 0.8], [6, 1]])
>>> from sklearn.decomposition import NMF
>>> model = NMF(n_components=2, init='random', random_state=0)
>>> W = model.fit_transform(X)
>>> H = model.components_

In the above code, fit(X), learns an NMF model for the input X. transform(X), transforms the data X according to the fitted NMF model.fit_transform(X), together learns an NMF model for the input X and returns transformed data.

NMF is often used in NLP tasks such as — topic modeling as well. When applied to news articles, it is able to generate the different topics of the articles. This can be used to recommend similar articles in online news portals catered to each reader. It is possible that the medium is applying NMF right now on your past reading data and thus recommending this blog to you according to your past interests! [haha, I don't know, ask them xd!!!!!]

That’s all from today. I hope you like my blog. See you in my next blog with some real and exciting topics related to data science, machine learning, and artificial intelligence. Do not forget to comment, give some claps and follow me if you like my articles! Your encouraging words and insightful comments inspire me to write more. Thank you so much for reading :)

--

--

Salman Ibne Eunus
CodeX
Writer for

Data Scientist|Robotics Engineer||AI Researcher| Bioinformatics