我正在尝试使用Python SciKit遵循GMM的本教程。问题是原始代码无法立即使用。它说输入数组的形状存在问题,现在不建议使用GMM。我试图将其重写为:
np.random.seed(2)
x = np.concatenate([np.random.normal(0, 2, 200),
np.random.normal(5, 5, 200),
np.random.normal(3, 0.5, 600)])
x = np.reshape(x, (-1, 1))
plt.hist(x, 80, normed=True)
plt.xlim(-10, 20)
clf = GaussianMixture(4, max_iter=500, random_state=3).fit(x)
xpdf = np.linspace(-10, 20, 1000)
xpdf = np.reshape(xpdf, (-1, 1))
density = np.exp(clf.score(xpdf))
plt.hist(x, 80, normed=True, alpha=0.5)
plt.plot(xpdf, density, '-r')
plt.xlim(-10, 20)
但是我还是得到了ValueError: x and y must have same first dimension
。据我所知,问题已经从数组的形状转移到density
变量的形状。但是我不确定到底发生了什么。任何人都可以对此有所了解吗?谢谢。
如果您检查density
问题的形状,将会更加清楚:
>>> density.shape
()
该score
方法返回所传递的整个数据集的对数似然,这仅仅是一个标量值。您需要score_samples
,这将提供每个单独点的对数似然率。
自编写教程以来,此处的API可能已更改-我不确定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句