[SciPy-User] curve_fit cannot accept a function with list or array as parameters?

Spark Liang sparkliang at gmail.com
Thu Jun 9 22:57:14 EDT 2011


It works! Josef, thank you!

Spark

On Thu, Jun 9, 2011 at 10:47 PM, <josef.pktd at gmail.com> wrote:

> On Thu, Jun 9, 2011 at 10:39 AM, Spark Liang <sparkliang at gmail.com> wrote:
> > Hi, I'm using scipy.optimize.curve_fit to fit two sets of data (x, y).
> But I
> > found that curve_fit cannot accept a function with list or numpy.ndarray
> as
> > parameters.
> > For example, one of my function is :
> > def testfunc(x, beta)
> >        a = beta[0]
> >        b = beta[1]
> >        c = beta[2]
> >        d = beta[3]
> >        return a+b*x+c*x**2+d*x**4
> > In my program, I create the parameters guess: c = [1, 2, 3].  When I
> using
> > curve_fit as: popt, pcov = curve_fit(testfunc, x, y, p0=c). It threw the
> > errors:  TypeError: testfunc() takes exactly 2 arguments (4 given).
> > How to resolve the problems ?
>
> add a * and it will unpack the iterable, array or list
> def testfunc(x, *beta)
>
> Josef
>
> >
> > _______________________________________________
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110610/b4489c73/attachment.html>


More information about the SciPy-User mailing list