Search code examples
pythonscipycurve-fittingcurve

curve fit with 2 input variables


Using scipy.optimize.curve_fit I'm trying to get a best fit function og 2 measured data series to a third measured data series, like f(x,y)=z, where x,y,z are the measured series.

The code goes:

def func_events_model(xy,a,b,c):
 return a*xy[0]+b*xy[1]+c
events_array=numpy.array(events_list)
Tin_array=numpy.array(Tin_list)
barometer_array=numpy.array(barometer_list)
events_array=events_array.reshape(720,1)
Tin_barometer_array=numpy.array([[Tin_list],[barometer_list]])
Tin_barometer_array=Tin_barometer_array.T
popt_model,stats_model=curve_fit(func_events_model,Tin_barometer_array,events_array)

I get this error message:

Traceback (most recent call last):
  File "DUKS_dataplot.py", line 100, in <module>
    popt_model,stats_model=curve_fit(func_events_model,Tin_barometer_array,events_array)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 506, in curve_fit
    res = leastsq(func, p0, args=args, full_output=1, **kw)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 355, in leastsq
    gtol, maxfev, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.

Any ideas to handle this? Or a better way to find best fit of f(x,y)=z?

The documentation for scipy.optimize.curve_fit states that the independent input may have multiple dimensions.


Solution

  • Changing the function definition to:

    def func_events_model(xy,a,b,c):
     return a*xy[0]+b*xy[1]+c
    

    did the trick.