python - Pass tuple as input argument for scipy.optimize.curve_fit -


i have following code:

import numpy np scipy.optimize import curve_fit   def func(x, p): return p[0] + p[1] + x   popt, pcov = curve_fit(func, np.arange(10), np.arange(10), p0=(0, 0))  

it raise typeerror: func() takes 2 arguments (3 given). well, sounds fair - curve_fit unpact (0, 0) 2 scalar inputs. tried this:

popt, pcov = curve_fit(func, np.arange(10), np.arange(10), p0=((0, 0),)) 

again, said: valueerror: object deep desired array

if left default (not specifying p0):

popt, pcov = curve_fit(func, np.arange(10), np.arange(10))   

it raise indexerror: invalid index scalar variable. obviously, gave function scalar p.

i can make def func(x, p1, p2): return p1 + p2 + x working, more complicated situations code going verbose , messy. i'd love if there's cleaner solution problem.

thanks!

not sure if cleaner, @ least easier add more parameters fitting function. maybe 1 make better solution out of this.

import numpy np scipy.optimize import curve_fit   def func(x, p): return p[0] + p[1] * x  def func2(*args):     return func(args[0],args[1:])  popt, pcov = curve_fit(func2, np.arange(10), np.arange(10), p0=(0, 0)) print popt,pcov 

edit: works me

import numpy np scipy.optimize import curve_fit  def func(x, *p): return p[0] + p[1] * x  popt, pcov = curve_fit(func, np.arange(10), np.arange(10), p0=(0, 0)) print popt,pcov 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -