scikitlearn中NMF中的自定义矩阵

2024-04-19 20:21:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我要用sklearn做NMF,我使用了这里的说明: http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.NMF.html

我想添加我的初始化矩阵H,有选项可以做init='custom',但我不知道如何给他矩阵H。 我试过:

model = NMF(n_components=2, init='custom',H=myInitializationH random_state=0);

但它不起作用。在

另外,是否有人知道如何修复我的矩阵,只更新W?在

编辑:

谢谢你的回答

当我选择自定义选项时,我得到错误:

^{pr2}$

然而,矩阵不包含任何nan或无穷大。 此外,我对非常小的矩阵进行了测试,看它是否良好:

import numpy as np
from sklearn.decomposition import NMF

x=np.ones((2,3));
#model = NMF(n_components=1, init='custom', solver='mu',beta_loss=1,max_iter=500,random_state=0,alpha=0,verbose=0, shuffle=False);
model = NMF(n_components=1, init='custom');
fixed_W = model.fit_transform(x,H=np.ones((1,3)));
fixed_H = model.components_;

print(np.matmul(fixed_W,fixed_H));

我也犯了同样的错误,除非我用“随机”而不是“自定义”。在

你也会这样吗?为什么会这样?在


Tags: importmodelinit选项错误customnpcomponents
1条回答
网友
1楼 · 发布于 2024-04-19 20:21:10

传递fit()fit_transform()中的W和H。在

根据documentation of ^{}

W : array-like, shape (n_samples, n_components)
    If init=’custom’, it is used as initial guess for the solution.

H : array-like, shape (n_components, n_features)
    If init=’custom’, it is used as initial guess for the solution.

这同样适用于fit()。在

做一些类似的事情:

model.fit(X, H=myInitializationH, W=myInitializationW)

更新: 似乎如果传递init='custom'参数,则需要同时提供W和H。如果提供H而不提供W,则会将其视为None,然后抛出一个错误。在

相关问题 更多 >