2016-03-01 91 views
0

Gaussian fit for Python的回答中是否需要更改任何内容以适合对数空间中的数据?具体而言,x和y的数据涵盖了几个数量级,这代码片段:高斯拟合对数空间

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') 

的“适合”是水平线,我不知道我是否丢失在我的代码的东西,或者如果我的数据根本不适合高斯。任何帮助将不胜感激!

回答

0

这是我失踪了:数据需要做安装前进行改造,再转换回对数轴绘制:

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')