[SciPy-User] Fitting a Gaussian

David Baddeley david_baddeley@yahoo.com...
Mon Jan 17 16:50:10 CST 2011

Sounds like you might be trying to fit a curve with a Gaussian, rather than find 
the parameters of a set of Gaussian distributed observations as Josef assumed. 
In this case you'll want to define a model function eg:

import numpy as np

def gauss(p, x):
    A, mu, sigma = p
    return A*np.exp(-(x-mu)**2/(2*sigma**2))

and then have a look at scipy.optimize.curvefit , or if you've got an older 
version of scipy scipy.optimize.leastsq (in which case you'll need a misfit 
function - ie y - f(x) instead


From: Benedikt Riedel <briedel@wisc.edu>
To: SciPy Users List <scipy-user@scipy.org>
Sent: Tue, 18 January, 2011 11:29:37 AM
Subject: Re: [SciPy-User] Fitting a Gaussian

Mygaussfit basically fits for the sigma, mu and A, for the function 
f(x)=Aexp(-(x-mu)^2/(2sigma^2)). What I am most interested is the A in this case 
and the sigma, since I am trying to compare two gaussians.

Thanks for the input.



On Mon, Jan 17, 2011 at 16:09, <josef.pktd@gmail.com> wrote:

On Mon, Jan 17, 2011 at 4:55 PM, Benedikt Riedel <briedel@wisc.edu> wrote:
>> Hi all,
>> I am a bit dumbfounded. I have been trying to figure out how to fit a
>> gaussian to a data set that I have. The only thing I have been able to dig
>> up with so far is that I have to get scipy to compute the mean and standard
>> deviation for me and then basically plug those into a gaussian. Just seems a
>> little bit odd to me considering that Octave has the mygaussfit function
>> included or is there an easier method?
>I don't know what a mygaussfit does, but we have this:
>>>> rvs = np.random.randn(200)
>>>> from scipy import stats
>>>> stats.norm.fit(rvs)
>(-0.0092383641087203858, 1.0567583206929831)
>>>> loc, scale = stats.norm.fit(rvs)
>>>> rvs.mean()
>>>> rvs.std()
>>>> loc, scale
>(-0.0092383641087203858, 1.0567583206929831)
>>>> myfrozennorm = stats.norm(loc=loc, scale=scale)
>>>> myfrozennorm.cdf(2)
>This assumes that all observations are identically distributed and
>independent from each other. Otherwise, it get's a little bit more
>> Cheers,
>> Ben
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>SciPy-User mailing list

Benedikt Riedel
Graduate Student University of Wisconsin-Madison
Department of Physics
Office: 2304 Chamberlin Hall
Lab: 6247 Chamberlin Hall
Tel:  (608) 301-5736
Cell: (213) 519-1771
Lab: (608) 262-5916

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20110117/a953e77f/attachment-0001.html 

More information about the SciPy-User mailing list