我在 python 中有一组数据。我将其绘制为直方图,该图显示双峰分布,因此我试图在双峰的每个峰上绘制两个高斯分布。
如果我使用下面的代码,则需要我有两个大小相同的数据集。但是我只有一个数据集,不能平分。我怎样才能适应这两个高斯
from sklearn import mixture
import matplotlib.pyplot
import matplotlib.mlab
import numpy as np
clf = mixture.GMM(n_components=2, covariance_type='full')
clf.fit(yourdata)
m1, m2 = clf.means_
w1, w2 = clf.weights_
c1, c2 = clf.covars_
histdist = matplotlib.pyplot.hist(yourdata, 100, normed=True)
plotgauss1 = lambda x: plot(x,w1*matplotlib.mlab.normpdf(x,m1,np.sqrt(c1))[0], linewidth=3)
plotgauss2 = lambda x: plot(x,w2*matplotlib.mlab.normpdf(x,m2,np.sqrt(c2))[0], linewidth=3)
plotgauss1(histdist[1])
plotgauss2(histdist[1])
原文由 astrochris 发布,翻译遵循 CC BY-SA 4.0 许可协议
这里使用 scipy 工具进行模拟:
数据是两个正态样本的叠加,模型是高斯曲线的总和。我们获得 :
估计参数是: