Non-negative matrix factorization in Sklearn

I apply non-negative matrix factorization (NMF) to a large matrix. In essence, the NMF method does the following: given m to n matrix A, NMF decomposes into A = WH, where W is m by d, and H is by n. The ProjectedGradientNMF method is implemented in the Python Sklearn package. I would like the algorithm to return both W and H. But it seems that it returns H, not W. Applying the algorithm again to AT (transpose) can give me W. However, I would like to avoid it twice, since the matrix ix very big.

If you could tell me how to get W and H at the same time, that would be great! Below is my code:

from sklearn.decomposition import ProjectedGradientNMF import numpy A = numpy.random.uniform(size = [40, 30]) nmf_model = ProjectedGradientNMF(n_components = 5, init='random', random_state=0) nmf_model.fit(A) H = nmf_model.components_.T 
+6
source share
1 answer

Fortunately, you can view the source code:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/nmf.py

fit_transform() starts at line 460, and line 530 shows that H bound to components_ , and the function W returned from the function.

Therefore, you do not need to run this twice, you just need to change:

 nmf_model.fit(A); H = nmf_model.components_.T; 

to

 W = nmf_model.fit_transform(A); H = nmf_model.components_; 
+18
source

Source: https://habr.com/ru/post/972181/


All Articles