Fit to function values with numpy/scipy

Machiel Kolstein mkolstein at ifae.es
Mon Nov 25 12:31:44 EST 2019


Okay, I found some answer myself: use scipy.optimize.curve_fit
However, I still find it strange that I have to define a gauss function myself instead of it being readily available. I did this: 

# Define model function to be used to fit to the data
def gauss(x, *p):
    A, mu, sigma = p
    return A*np.exp(-(x-mu)**2/(2.*sigma**2))
p0 = [1., 0., 1.]

# Fit the histogram -----------------------------
coeff, var_matrix = curve_fit(gauss, x_array, y_array, p0=p0)
amplitude, mu, sigma = coeff
print "amplitude, mu, sigma = ", amplitude, mu, sigma

# Get the fitted curve
hist_fit = gauss(x_array, *coeff)
plt.plot(x_array, hist_fit, color='red', linewidth=5, label='Fitted data')

plt.show()

-- 
Avís -
Aviso - Legal Notice - (LOPD) - http://legal.ifae.es 
<http://legal.ifae.es/>


More information about the Python-list mailing list