[SciPy-User] optimize.leastsq and improper input parameters
ms
devicerandom at gmail.com
Thu Jul 8 13:46:07 EDT 2010
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 at 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?
Thanks a lot,
Massimo
More information about the SciPy-User
mailing list