Wolfram Function Repository
Instantuse addon functions for the Wolfram Language
Function Repository Resource:
Decompose a matrix into two nonnegative matrix factors
ResourceFunction["NonNegativeMatrixFactorization"][mat,k] finds nonnegative matrix factors for the matrix mat using k dimensions. 
"Epsilon"  10^{9}  denominator (regularization) offset 
MaxSteps  200  maximum iteration steps 
"NonNegative"  True  should the nonnegativity be enforced at each step 
"Normalization"  Left  which normalization is applied to the factors 
PrecisionGoal  Automatic  precision goal 
"ProfilingPrints"  False  should profiling times be printed out during execution 
"RegularizationParameter"  0.01  regularization (multiplier) parameter 
Create a random integer matrix:
In[1]:= 

Out[3]= 

Compute the NNMF factors:
In[4]:= 

Out[5]= 

Here is the matrix product of the obtained factors:
In[6]:= 

Out[6]= 

Note that elementwise relative errors between the original matrix and reconstructed matrix are small:
In[7]:= 

Out[7]= 

Here is a random matrix with its first two columns having much larger magnitudes than the rest:
In[8]:= 

Out[12]= 

Here we compute NNMF factors:
In[13]:= 

Find the relative error of the approximation by the matrix factorization:
In[14]:= 

Out[14]= 

Here is the relative error for the first three columns:
In[15]:= 

Out[15]= 

Here are comparison plots:
In[16]:= 

Out[16]= 

NNMF factors can be normalized in two ways: (i) the Euclidean norms of the columns of the left factor are all equal to 1; or (ii) the Euclidean norms of the rows of the right factor are all equal to 1. Here is a table that shows NNMF factors for different normalization specifications:
In[17]:= 

Out[17]= 

The implemented NNMF algorithm uses the gradient descent algorithm. The regularization parameter controls the “learning rate” and can have dramatic influence on the number of iteration steps and approximation precision. Compute NNMF with different regularization multiplier parameters:
In[18]:= 

Plot the results:
In[19]:= 

Out[19]= 

Here are the corresponding norms:
In[20]:= 

Out[20]= 

One of the main motivations for developing NNMF algorithms is the easier interpretation of extracted topics in the framework of latent semantic analysis. The following code illustrates the extraction of topics over a dataset of movie reviews.
Start with a movie reviews dataset:
In[21]:= 

Out[22]= 

Change the labels “positive” and “negative” to have the prefix “tag:”:
In[23]:= 

Concatenate to each review the corresponding label and create an Association:
In[24]:= 

Out[25]= 

Select movie reviews that are nontrivial:
In[26]:= 

Out[27]= 

Split each review into words and delete stopwords:
In[28]:= 

Convert the movie reviews association into a list of IDword pairs (“long form”):
In[29]:= 

Out[30]= 

Replace each word with its Porter stem:
In[31]:= 

Out[31]= 

Find the frequency of appearance of all unique word stems and pick words that appear in more than 30 reviews:
In[32]:= 

Out[34]= 

Filter the IDword pairs list to contain only words that are sufficiently popular:
In[35]:= 

Compute an IDword contingency matrix:
In[36]:= 

Here is a sample of the contingency matrix as a dataset:
In[37]:= 

Out[38]= 

Visualize the contingency matrix:
In[39]:= 

Out[40]= 

Take the contingency sparse matrix:
In[41]:= 

Here is a plot that shows the Pareto principle adherence of the selected popular words:
In[42]:= 

Out[42]= 

Judging from the Pareto plot we should apply the inverse document frequency (IDF) formula:
In[43]:= 

Normalize each row with the Euclidean norm:
In[44]:= 

In order to get computations faster, we take a sample submatrix:
In[45]:= 

Out[46]= 

Apply NNMF in order to extract 24 topics using 12 maximum iteration steps, normalizing the right factor:
In[47]:= 

Out[48]= 

Show the extracted topics using the right factor (H):
In[49]:= 

Out[49]= 

Here we show statistical thesaurus entries for random words, selected words and the labels (“tag:positive”,”tag:negative”):
In[50]:= 

Out[53]= 

NNMF should be compared with the singular value decomposition (SVD) and independent component analysis (ICA).
Generate 3D points:
In[54]:= 

Compute matrix factorizations using NNMF, SVD and ICA and make a comparison plots grid (rotate the graphics boxes for better perception):
In[55]:= 

Out[62]= 

This work is licensed under a Creative Commons Attribution 4.0 International License