2016-03-01 2 views
0

Будет ли что-нибудь нужно изменить в ответе для Gaussian fit for Python, чтобы соответствовать данным в лог-лог-пространстве? В частности, для обоих х и у данных, охватывающих несколько порядков, и этот фрагмент кода:Гауссовская посадка в лог-лог-пространстве

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

 Смежные вопросы

  • Нет связанных вопросов^_^