On 11/25/19 12:31 PM, Machiel Kolstein wrote:
> 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()
>
That actually solves a slightly different problem. norm.fit basically
computes the mean and standard deviation of the data (and if you know
what the mean should be, you can provide that to get a better estimate
of the standard deviation). If you problem really is a weighted data
problem, then it isn't hard to compute a weighted average or weighted
standard deviation, I don't know scipy to know if it has something like
that built in. A quick scan shows that numpy.average allows a weighting
array to be provided, so it shouldn't be hard to look at the code for
sci[y.norm.fit and convert it to use weighted averages.

-- 
Richard Damon

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to