【问题标题】:custom matrix in NMF in scikit-learnscikit-learn 中 NMF 中的自定义矩阵
【发布时间】:2018-09-25 18:53:46
【问题描述】:

我必须使用 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 吗?

编辑:

谢谢你的回答

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

ValueError: input contains nan infinity or a value too large for dtype('float64')

但是,矩阵不包含任何 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));

除非我执行“随机”而不是“自定义”,否则我会遇到同样的错误。

你也遇到过这种情况吗?为什么会这样?

【问题讨论】:

  • 错误信息是什么?
  • H=myInitializationH 后面少了一个逗号,可能是这个问题?

标签: python optimization scikit-learn nmf


【解决方案1】:

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

根据documentation of fit_transform():-

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,然后抛出错误。

【讨论】:

  • @kalonymus 这取决于您的数据。你的数据是否正确。根据错误,它包含一些缺失的信息或一些无限的值。
  • 数据是正确的,我用 3X2 矩阵做了测试用例,只有 1 个组件,它仍然有问题。请查看编辑后的帖子
  • @kalonymus 如果对您有帮助,请接受答案。
  • 我做到了。我也为你投票,但它没有公开显示,因为这是新用户,我没有足够的声誉。无论如何,你是完全应得的。谢谢!
猜你喜欢
  • 2017-07-10
  • 2016-05-12
  • 2018-05-22
  • 2016-02-27
  • 2015-08-04
  • 2020-07-16
  • 1970-01-01
  • 2019-05-29
  • 2018-08-26
相关资源
最近更新 更多