Python多高斯拟合-值误差:GMM估计有2个分量,但只得到1个样本



我有两个想要拟合的高斯分布。由于这两种分布可以以不同的方式混合,我希望拟合尽可能通用。我在这里找到了下面的代码:

python中直方图数据的高斯拟合:信任区域v/s Levenberg-Marquardt-第一个答案。

然而,它不适用于我的数据或下面代码中生成的原始数据,并抛出错误:

ValueError: GMM estimation with 2 components, but got only 1 samples

我希望它是简单的。我的数据只是一个2D数组,它绘制了时间与振幅的直方图。

import numpy as np
from sklearn import mixture
import matplotlib.pyplot as plt
comp0 = np.random.randn(1000) - 5 # samples of the 1st component
comp1 = np.random.randn(1000) + 5 # samples of the 2nd component
x = np.hstack((comp0, comp1)) # merge them
gmm = mixture.GMM(n_components=2) # gmm for two components
gmm.fit(x) # train it!
linspace = np.linspace(-10, 10, 1000)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.hist(x, 100) # draw samples
ax2.plot(linspace, np.exp(gmm.score_samples(linspace)[0]), 'r') 
plt.show()

使用:

x = np.vstack((comp0, comp1))

而不是hstack

因为每一行都应该表示一个样本,而每一列都是样本的特征。

最新更新