# [SciPy-User] bug in optimize.curve_fit ?

josef.pktd@gmai... josef.pktd@gmai...
Fri Aug 5 03:53:29 CDT 2011

```On Fri, Aug 5, 2011 at 4:39 AM,  <josef.pktd@gmail.com> wrote:
> On Fri, Aug 5, 2011 at 3:29 AM, Xavier Gnata <xavier.gnata@gmail.com> wrote:
>> Hi,
>>
>> Yes but the doc (
>> http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
>> ) claims that xdata can be "An N-length sequence or an (k,N)-shaped array"
>>
>> IRL, I would not cast a array to a list to call optimize.curve_fit.
>> x = list(x) was only an easy way to write a short testcase based on the
>> exemple in the doc.
>>
>> Xavier
>>
>>> Hi,
>>>
>>> if you did x = list(x) it becomes a simple list, which cannot be
>>> multiplied by a number.
>>> don't do this.
>>>
>>> --
>>> Oleksandr Huziy
>>>
>>>
>>> 2011/8/4 Xavier Gnata <xavier.gnata@gmail.com
>>> <mailto:xavier.gnata@gmail.com>>
>>>
>>>     Hi,
>>>
>>>     def func(x, a, b, c):
>>>        return a*np.exp(-b*x) + c
>>>     x = np.linspace(0,4,50)
>>>     y = func(x, 2.5, 1.3, 0.5)
>>>     yn = y + 0.2*np.random.normal(size=len(x))
>>>     popt, pcov = curve_fit(func,x, yn)
>>>
>>>     works vey well but if you change it to:
>>>
>>>     def func(x, a, b, c):
>>>        return a*np.exp(-b*x) + c
>>>     x = list(np.linspace(0,4,50))
>>>     y = func(x, 2.5, 1.3, 0.5)
>>>     yn = y + 0.2*np.random.normal(size=len(x))
>>>     popt, pcov = curve_fit(func, x, yn)
>>>
>>>     then x is a list and we get this error:
>>>     "TypeError: can't multiply sequence by non-int of type 'float'"
>>>
>>>     However, according to the documentation, xdata : An N-length sequence
>>>     or an (k,N)-shaped array. I understand this statement as : "either a
>>>     list, a tuple or an array". Should optimize.curve_fit internally cast
>>>     xdata to an array? I would think so.
>
> The docstring might be a bit misleading. x (xdata) is just handed
> through curve_fit and leastsq to your function and could be anything.
> The interpretation as xdata is just for the specific usecase
> y=f(x)+noise, but nothing in the implementation requires directly
> anything about x. (The only requirement is that f(x) returns a (N,)
> array for an x.)

for example:

import numpy as np
from scipy.optimize import curve_fit

def func(x, a, b, c):
#print b, x, type(x)
return a*np.exp(-b*x.x) + c

x0 = list(np.linspace(0,4,50))

class Dummy(object):
def __init__(self, x):
self.x = np.asarray(x)

xd = Dummy(x0)

y = func(xd, 2.5, 1.3, 0.5)
yn = y + 0.2*np.random.normal(size=len(x0))
popt, pcov = curve_fit(func, xd, yn)

print popt

Josef

>
> So, I don't think curve_fit should do any changes to x, it's an arg
> for the user function that the user should take care of, and a user
> can exploit it's flexibility.
>
> Josef
>
>
>>>
>>>
>>>
>>>     Xavier
>>>     _______________________________________________
>>>     SciPy-User mailing list
>>>     SciPy-User@scipy.org <mailto:SciPy-User@scipy.org>
>>>     http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>>
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
```