[SciPy-user] optimize.leastsq - Value Error: shape mismatch

David Trethewey dlrt2@ast.cam.ac...
Fri Oct 24 09:31:52 CDT 2008


Traceback (most recent call last):
 File "M31FeHfit_total.py", line 86, in <module>
   p1,success  = optimize.leastsq(errfunc, p0, args = (X, Y))
 File "/usr/lib/python2.5/site-packages/scipy/optimize/minpack.py", line 
264, in leastsq
   m = check_func(func,x0,args,n)[0]
 File "/usr/lib/python2.5/site-packages/scipy/optimize/minpack.py", line 
11, in check_func
   res = atleast_1d(thefunc(*((x0[:numinputs],)+args)))
 File "M31FeHfit_total.py", line 81, 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

when running the following code:

   hista = hist(FeH_sub_range,bins)
    print "hista[1] = ",hista[1]
    print "hista[0] = ",hista[0]
    X = numpy.array(hista[1])
    Y = numpy.array(hista[0])
    print X
#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
       p0 = numpy.array([10.0,-2,0.5])
    p1,success  = optimize.leastsq(errfunc, p0, args = (X, Y))

As I am passing the arguments as numpy arrays I don't see why there is a 
problem. Additionally this problem has arisen since changing version of 
Python.

David Trethewey


More information about the SciPy-user mailing list