[SciPy-User] Bug of sp.optimize.curve_fit

Warren Weckesser warren.weckesser@enthought....
Thu Jun 17 08:13:39 CDT 2010


Looks like a bug.  I filed a ticket here:
    http://projects.scipy.org/scipy/ticket/1204

Here's another example to reproduce the problem:

-----

In [5]: def func(x, a):
   ...:     y = x**a
   ...:     return y
   ...:

In [6]: x = np.array([2.0, 5.0, 6.0])

In [7]: y = np.array([4.5, 24.0, 37.5])

In [8]: curve_fit(func, x, y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/warren/Desktop/<ipython console> in <module>()

/Library/Frameworks/Python.framework/Versions/6.1/lib/python2.6/site-packages/scipy/optimize/minpack.pyc 
in curve_fit(f, xdata, ydata, p0, sigma, **kw)
    423
    424     if (len(ydata) > len(p0)) and pcov is not None:
--> 425         s_sq = (func(popt, *args)**2).sum()/(len(ydata)-len(p0))
    426         pcov = pcov * s_sq
    427     else:

/Library/Frameworks/Python.framework/Versions/6.1/lib/python2.6/site-packages/scipy/optimize/minpack.pyc 
in _general_function(params, xdata, ydata, function)
    337
    338 def _general_function(params, xdata, ydata, function):
--> 339     return function(xdata, *params) - ydata
    340
    341 def _weighted_general_function(params, xdata, ydata, function, 
weights):

TypeError: func() argument after * must be a sequence, not numpy.float64

In [9]: def func2(x, a, b):
   ...:     y = b * x**a
   ...:     return y
   ...:

In [10]: curve_fit(func2, x, y)
Out[10]:
(array([ 2.17102543,  0.75638651]),
 array([[ 0.0848126 , -0.11091404],
       [-0.11091404,  0.1456962 ]]))

-----



Warren

Martin Paesold wrote:
> Hi,
>
> I ran into trouble as I tried to fit data using only one fitting
> parameter.  The functions '_general_function' and
> '_weigted_general_function' in the module
> python2.6/site-packages/scipy/optimize/minpack.py throw an TypeError.
>
> I use Python 2.6.5 -- EPD 6.2-1 (32-bit) on Ubuntu 9.10
>
> I attached a file that produces the error. I think the problem is that
> the argument 'params' of the above functions is passed to the model
> used for the fit as 'function(xdata, *params)'. It seems that 'params'
> can be scalar which causes the TypeError when calling 'function'. I
> don't see why that happens, but for now I could solve my problem if
> '_general_function' and '_weigted_general_function' check whether
> 'params' is scalar and cast it to a list if so:
> 'if isscalar(params): params = [params]'.
>
> Cheers,
>
> Martin Paesold
> martin.paesold@gmail.com
>
> 6 Clementi Road
> #01-07 Amicus Student Hostel
> Singapore 129 741
>
> +65 9448 8914
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-User mailing list