[SciPy-user] Problem with scipy.optimize.leastsq: Improper input parameters

Adam Ginsburg adam.ginsburg@colorado....
Sat Mar 8 16:27:36 CST 2008


I'm not sure if my previous e-mail (below) went out earlier, but I've spent
a while trying to understand what the problem really meant, and I think it's
that I'm trying to use leastsq for something it was not intended for.
However, the Levenberg-Marquardt algorithm should be capable of this task.

Specifically, I'm trying to minimize a function of 5 variables, while
leastsq expects the number of functions m to be larger than the number of
variables n.  I figured out that my arg() variable(s?) determine the size of
m, which seems like a bad idea to me but I can deal with it.  My new error,
then, is a claim that my 2d array somehow doesn't work (traceback below).
Am I wrong in thinking that the function should be able to minimize over a
2D space, or am I just not specifying that properly in my function call?

Thanks, and sorry to be flooding the list a little,
Adam

---------------------------------------------------------------------------
<type 'exceptions.ValueError'>            Traceback (most recent call last)


<type 'exceptions.ValueError'>: object too deep for desired array
Error in sys.excepthook:
Traceback (most recent call last):
  File "/sw/lib/python2.5/site-packages/IPython/iplib.py", line 1714, in
excepthook
    self.showtraceback((etype,value,tb),tb_offset=0)
  File "/sw/lib/python2.5/site-packages/IPython/iplib.py", line 1514, in
showtraceback
    self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
  File "/sw/lib/python2.5/site-packages/IPython/ultraTB.py", line 872, in
__call__
    self.debugger()
  File "/sw/lib/python2.5/site-packages/IPython/ultraTB.py", line 729, in
debugger
    while self.tb.tb_next is not None:
AttributeError: 'NoneType' object has no attribute 'tb_next'

Original exception was:
ValueError: object too deep for desired array
---------------------------------------------------------------------------
<class 'minpack.error'>                   Traceback (most recent call last)

/Users/adam/classes/probstat/hw7.py in <module>()
     52 #a,b,x0,y0,s = fmin(chi2gaussnoglob,x0in,args=[im,RON])
     53 #print "Parameters a: %f   b: %f  x0: %f  y0: %f  sigma: %f   chi2:
%f" % (a,b,x0,y0,s,chi2gaussnoglob([a,b,x0,y0,s],im,RON))
---> 54 leastsq(chi2gaussnoglob,x0in,args=(im),Dfun=None)
     55 #leastsq(chi2gauss,x0in,maxfev=1000)
     56

/sw/lib/python2.5/site-packages/scipy/optimize/minpack.py in leastsq(func,
x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn,
factor, diag, warning)
    267         if (maxfev == 0):
    268             maxfev = 200*(n+1)
--> 269         retval =
_minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
    270     else:
    271         if col_deriv:

<class 'minpack.error'>: Result from function call is not a proper array of
floats.
> /sw/lib/python2.5/site-packages/scipy/optimize/minpack.py(269)leastsq()
    268             maxfev = 200*(n+1)
--> 269         retval =
_minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
    270     else:

On Sat, Mar 8, 2008 at 1:39 PM, Adam Ginsburg <adam.ginsburg@colorado.edu>
wrote:

> Hi, I've been trying to get the Levenberg-Marquardt minimization routine
> leastsq to work and have received the error "Improper input parameters" when
> using my own non-trivial function.
>
> The function I'm trying to minimize:
>
> def chi2gauss(ARR):
>     a,b,x0,y0,s = ARR
>     noisemap = sqrt(im+RON**2)
>     mychi2 = (( ( im - gi(a,b,x0,y0,s,im) ) / noisemap )**2).sum()
>     return mychi2
>
> def gi(a,b,x0,y0,s,im):
>     myx,myy = indices(im.shape)
>     gi = b + a * exp ( - ( (myx-x0)**2 + (myy-y0)**2)/(2*s**2) )
>     return gi
>
> where in this case im and RON are global variable, though I have also
> tested with im and RON specified as input parameters using the args=() input
> for leastsq.
>
> example:
>
> In [123]: leastsq(chi2gauss,[1,1,1,1,1],full_output=1)
> 5 1 1 0.000000 0.000000 0.000000 1200 100.000000Out[123]:
> (array([ 1.,  1.,  1.,  1.,  1.]),
>  None,
>  {'fjac': array([[ 0.],
>        [ 0.],
>        [ 0.],
>        [ 0.],
>        [ 0.]]),
>   'fvec': 24222.5789746,
>   'ipvt': array([0, 0, 0, 0, 0]),
>   'nfev': 0,
>   'qtf': array([ 0.,  0.,  0.,  0.,  0.])},
>  'Improper input parameters.',
>  0)
>
> Can anyone help me out?  What about my input is improper?
>
> Thanks,
> Adam
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080308/1a19e716/attachment.html 


More information about the SciPy-user mailing list