Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对数空间中的高斯拟合_Python_Scipy_Gaussian - Fatal编程技术网

Python 对数空间中的高斯拟合

Python 对数空间中的高斯拟合,python,scipy,gaussian,Python,Scipy,Gaussian,为了将数据放入日志空间,答案中是否需要更改任何内容?具体而言,对于涵盖几个数量级的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_va

为了将数据放入日志空间,答案中是否需要更改任何内容?具体而言,对于涵盖几个数量级的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')

“拟合”是水平线,我不确定我的代码中是否缺少某些内容,或者我的数据是否完全不适合高斯分布。任何帮助都将不胜感激

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

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