# [SciPy-User] optimize.leastsq and improper input parameters

josef.pktd@gmai... josef.pktd@gmai...
Thu Jul 8 15:50:50 CDT 2010

```On Thu, Jul 8, 2010 at 1:46 PM, ms <devicerandom@gmail.com> wrote:
> Hi,
>
> I am stuck with optimize.leastsq.
> I am writing a quite complicated code that fits data to two variations
> of the same function, each of those can be with or without some
> parameters fixed. The concept is:
> - write the two variations
> - have a list of what parameters will be fixed and which not
> - use reasonable starting points of the non-fixed params to fit, and
> keep the fixed params as fixed within the function
> - then have a generalized routine that has as input the function to
> actually fit, among other things
> - apply leastsq on that function
>
> What comes out is similar, in structure, to this simplified example:
> -----
> import scipy as sp
> import scipy.optimize as opt
> import numpy as np
>
>
> #Initial data
> x = np.arange(0,10,1)
> y = [i**2 for i in x]
>
> #Three nice functions to fit
> def xexponent1(param,i):
>     exp = param[0]
>     return i**exp
>
> def xexponent2(param, i):
>     exp_a=param[0]
>     exp_b=param[1]
>     return i**(exp_a) + i**(exp_b)
>
> def line(param,i):
>     A = param[0]
>     b = param[1]
>     return i*b + A
>
>
> def f_to_minimize(pars,args):
>     #Generalized function we use to minimize the fit
>     #calculates function and squared residuals
>     function,x,y = args[0],args[1],args[2]
>     y_estimate = [function(pars,xi) for xi in x]
>
>     #calculate squared residuals
>     resid = [(i-j)**2 for i,j in zip(y,y_estimate)]
>     return sum(resid)
>
> def minimize(x,y,func,p0):
>     #calls minimization
>     args = [func,x,y]
>     i = opt.leastsq(f_to_minimize, p0, args)
>     print i
>
> minimize(x,y,line,[1,2])
> -----
>
> Here you can play with p0 and the function to give to f_to_minimize as
> an argument.
>
> What comes out is that if I give a p0 = [1] and I use a single-variable
> function, it works. As soon as I try a two-variable function (and thus I
> need two input parameters), I get:
>
> massimo@boltzmann:~/work\$ python test_norm_leastsq.py
> Traceback (most recent call last):
>   File "test_norm_leastsq.py", line 44, in <module>
>     minimize(x,y,[1,2])
>   File "test_norm_leastsq.py", line 41, in minimize
>     i = opt.leastsq(f_to_minimize, p0, args)
>   File "/usr/lib/python2.6/dist-packages/scipy/optimize/minpack.py",
> line 300, in leastsq
>     raise errors[info][1], errors[info][0]
> TypeError: Improper input parameters.
>
> The funny thing is that it worked *before* I messed with the thing to
> simplify the function-choosing mechanism (before I had N different
> functions for each combination of fixed/nonfixed params, now I just have
> two and I fix stuff *inside* the function), and I can't see however how
> can this be different. Also, the example above leaves me perplexed -it
> seems leastsq simply doesn't want two-variable functions to be minimized
> in this case. Any hint?

change in def f_to_minimize(pars,args):

#calculate squared residuals
##    resid = [(i-j)**2 for i,j in zip(y,y_estimate)]
##    return sum(resid)
return [(i-j) for i,j in zip(y,y_estimate)]

leastsq does the squared sum itself, and needs as return of the
function the vector of residuals

docstring:
func – A Python function or method which takes at least one
(possibly length N vector) argument and returns M floating point numbers

with only one parameter leastsq assumes you have one observation and
one parameter N=1, M=1

this is what I get running the changed script
(array([-12.,   9.]), 2)

I didn't check anything else in your script.

Josef

>
> Thanks a lot,
> Massimo
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```