SciKit Gaussian Mixture Model ValueError: x and y must have same first dimension

SciKit Gaussian Mixture Model ValueError: x and y must have same first dimension

我正在尝试使用 Python SciKit 遵循此 tutorial 的 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 方法 returns 它传递的 整个数据集 的对数似然,它只是一个标量值。您需要 score_samples,这将提供每个单独点的对数似然。

自教程编写以来,API 可能已在此处更改 -- 我不确定。