简体   繁体   中英

Scipy curve_fit multiple series of data


I'm trying to have a curve fit that takes into account multiple series of y based on same values of x and same (exponential) law. The y values among the series vary a little since they're experimental but are still close (at same x).

I tried to build two arrays: one with the x and one with the two different series of y

def f(x,a,b,c):
    return a*numpy.exp(-b*x)+c
xdata=numpy.array([data['x'],data['x']])
ydata = numpy.array([data['y1'], data['y2']])
popt, pcov=curve_fit(f,xdata,ydata)

But this error appears:

TypeError: Improper input: N=3 must not exceed M=2

Does anyone know how solve this error or a proper way to do this kind of curve fitting?

You should concatenate the data properly instead of creating a multi-dimensional array. There is nothing in curve_fit that states that the data has to be sorted by x :

xdata = np.concatenate((data['x'], data['x']))
ydata = np.concatenate((data['y1'], data['y2']))
popt, pcov = curve_fit(f, xdata, ydata)

This assumes that the referenced elements of data are all 1D.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM