fit two gaussians to a histogram from one set of data, python

前端 未结 1 1385
情歌与酒
情歌与酒 2020-12-13 07:55

I have one set of data in python. I am plotting this as a histogram, this plot shows a bimodal distribution, therefore I am trying to plot two gaussian profiles over each pe

相关标签:
1条回答
  • 2020-12-13 08:09

    Here a simulation with scipy tools :

    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)    
    

    The data is the superposition of two normal samples, the model a sum of Gaussian curves. we obtain :

    And the estimate parameters are :

    # 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
    
    0 讨论(0)
提交回复
热议问题