【问题标题】:Gaussian fit in log-log space对数空间中的高斯拟合
【发布时间】:2016-03-02 02:53:18
【问题描述】:

在Gaussian fit for Python 的答案中是否需要更改任何内容以适应日志-日志空间中的数据?具体来说,对于覆盖几个数量级的x和y数据以及这段代码sn-p:

from scipy.optimize import curve_fit
from scipy import asarray as ar,exp

def gaus(x,a,x0,sigma):
    return a*exp(-(x-x0)**2/(2*sigma**2))

b=np.genfromtxt('Stuff.dat', delimiter=None, filling_values=0)
x = b[:,0]
y = b[:,1] 
n = len(x)                          #the number of data
mean = sum(x*y)/n                   #note this correction
sigma = sum(y*(x-mean)**2)/n      #note this correction
popt,pcov = curve_fit(gaus,x,y,p0=[max(y),mean,sigma])
ax = pl.gca()
ax.plot(x, y, 'r.-')
ax.plot(x,gaus(x,*popt),'ro:')
ax.set_xscale('log')
ax.set_yscale('log')

“拟合”是水平线,我不确定我的代码中是否遗漏了某些内容,或者我的数据是否根本不适合高斯。任何帮助将不胜感激!

【问题讨论】:

    标签: python scipy gaussian


    【解决方案1】:

    这就是我所缺少的:在进行拟合之前需要转换数据,然后转换回对数轴上的绘图:

    from scipy.optimize import curve_fit
    from scipy import asarray as ar,exp
    import numpy as np
    
    def gaus(x,a,x0,sigma):
        return a*exp(-(x-x0)**2/(2*sigma**2))
    
    b=np.genfromtxt('Stuff.dat', delimiter=None, filling_values=0)
    x = np.log(b[:,0])
    y = np.log(b[:,1]) 
    n = len(x)                          #the number of data
    mean = sum(x*y)/n                   #note this correction
    sigma = sum(y*(x-mean)**2)/n      #note this correction
    popt,pcov = curve_fit(gaus,x,y,p0=[max(y),mean,sigma])
    ax = pl.gca()
    ax.plot(x, y, 'r.-')
    ax.plot(10**x,10**(gaus(x,*popt)),'ro:')
    ax.set_xscale('log')
    ax.set_yscale('log')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 2020-10-25
      • 2017-08-30
      • 2011-12-06
      • 2013-10-12
      • 2017-06-14
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多