[SciPy-user] optimize.leastsq

josef.pktd@gmai... josef.pktd@gmai...
Tue Jan 20 09:34:27 CST 2009


On Tue, Jan 20, 2009 at 6:30 AM, David Trethewey <dlrt2@ast.cam.ac.uk> wrote:
> I'm using the following code to fit a gaussian to a histogram of some data.
>
> #fit gaussian
>    fitfunc = lambda p, x: (p[0]**2)*exp(-(x-p[1])**2/(2*p[2]**2))  #
> Target function
>    errfunc = lambda p, x, y: fitfunc(p,x) -y          # Distance to the
> target function
>    doublegauss = lambda q,x: (q[0]**2)*exp(-(x-q[1])**2/(2*q[2]**2)) +
> (q[3]**2)*exp(-(x-q[4])**2/(2*q[5]**2))
>    doublegausserr = lambda q,x,y: doublegauss(q,x) - y
>    # initial guess
>    p0 = [10.0,-2,0.5]
>    # find parameters of single gaussian
>    p1,success  = optimize.leastsq(errfunc, p0[:], args =
> (hista[1],hista[0]))
>    errors_sq = errfunc(p1,hista[1],hista[0])**2
>
>
> I have the error message
>
> Traceback (most recent call last):
>  File "M31FeHfit_totalw108.py", line 116, in <module>
>   p1,success  = optimize.leastsq(errfunc, p0[:], args =
> (hista[1],hista[0]))
>  File "C:\Python25\lib\site-packages\scipy\optimize\minpack.py", line
> 264, in leastsq
>   m = check_func(func,x0,args,n)[0]
>  File "C:\Python25\lib\site-packages\scipy\optimize\minpack.py", line
> 11, in check_func
>   res = atleast_1d(thefunc(*((x0[:numinputs],)+args)))
>  File "M31FeHfit_totalw108.py", line 110, in <lambda>
>   errfunc = lambda p, x, y: fitfunc(p,x) -y          # Distance to the
> target function
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
> Anyone know why this happens? Curiously I have had it work before but
> not with my current versions of scipy and python etc.
>
> David

Check the dimensions hista[1],hista[0]. I can run your part of the
code without problems.

If you want to estimate the parameters of a (parametric) distribution,
then using maximum likelihood estimation would be more appropriate
than using least squares on the histogram.

Josef


More information about the SciPy-user mailing list