gpt4 book ai didi

python - 将两个高斯/正态分布的混合拟合到一组数据中的直方图,python

转载 作者:太空狗 更新时间:2023-10-29 17:39:37 29 4
gpt4 key购买 nike

我在 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])

最佳答案

这里是用 scipy 工具的模拟:

from pylab import *
from scipy.optimize import curve_fit

data=concatenate((normal(1,.2,5000),normal(2,.2,2500)))
y,x,_=hist(data,100,alpha=.3,label='data')

x=(x[1:]+x[:-1])/2 # for len(x)==len(y)

def gauss(x,mu,sigma,A):
return A*exp(-(x-mu)**2/2/sigma**2)

def bimodal(x,mu1,sigma1,A1,mu2,sigma2,A2):
return gauss(x,mu1,sigma1,A1)+gauss(x,mu2,sigma2,A2)

expected=(1,.2,250,2,.2,125)
params,cov=curve_fit(bimodal,x,y,expected)
sigma=sqrt(diag(cov))
plot(x,bimodal(x,*params),color='red',lw=3,label='model')
legend()
print(params,'\n',sigma)

数据是两个正态样本的叠加,模型是高斯曲线的总和。我们获得:

gauss with legend

估计参数是:

# via pandas :
# pd.DataFrame(data={'params':params,'sigma':sigma},index=bimodal.__code__.co_varnames[1:])
params sigma
mu1 0.999447 0.002683
sigma1 0.202465 0.002696
A1 226.296279 2.597628
mu2 2.003028 0.005036
sigma2 0.193235 0.005058
A2 117.823706 2.658789

关于python - 将两个高斯/正态分布的混合拟合到一组数据中的直方图,python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990467/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com