Scipy curve_fit multiple series of data

萝らか妹 提交于 2019-12-10 22:19:58

问题



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?


回答1:


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.



来源:https://stackoverflow.com/questions/43879801/scipy-curve-fit-multiple-series-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!