[SciPy-User] bug in optimize.curve_fit ?
josef.pktd at gmail.com
josef.pktd at gmail.com
Fri Aug 5 04:53:29 EDT 2011
On Fri, Aug 5, 2011 at 4:39 AM, <josef.pktd at gmail.com> wrote:
> On Fri, Aug 5, 2011 at 3:29 AM, Xavier Gnata <xavier.gnata at 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 at gmail.com
>>> <mailto:xavier.gnata at 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 at scipy.org <mailto:SciPy-User at scipy.org>
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>>
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
More information about the SciPy-User
mailing list