use*_*497 7 python machine-learning matrix scikit-learn
我在大矩阵上应用非负矩阵分解(NMF).基本上NMF方法如下:给定m乘n矩阵A,NMF分解成A = WH,其中W是m乘以d,H是d乘以n.ProjectedGradientNMF方法在Python包Sklearn中实现.我希望算法返回W和H.但它似乎只返回H,而不是W.再次将算法应用于AT(转置)可以给我W.但是,我想避免计算它两次,因为矩阵ix非常大.
如果你能告诉我如何同时获得W和H,那就太棒了!以下是我的代码:
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
Run Code Online (Sandbox Code Playgroud)
小智 19
幸运的是,您可以查看源代码:https:
//github.com/scikit-learn/scikit-learn/blob/master/sklearn/decomposition/nmf.py
fit_transform()从第460行开始,在第530行,它显示H附加到函数components_并W从函数返回.
所以你不应该两次运行,你应该改变:
nmf_model.fit(A);
H = nmf_model.components_.T;
Run Code Online (Sandbox Code Playgroud)
至
W = nmf_model.fit_transform(A);
H = nmf_model.components_;
Run Code Online (Sandbox Code Playgroud)