0

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

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

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

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

我希望它是简单的。我的数据只是一个绘制直方图、时间与幅度的二维数组。

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()
4

1 回答 1

1

利用:

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

代替hstack

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

于 2015-12-03T17:33:58.000 回答