为了账号安全,请及时绑定邮箱和手机立即绑定

使用curve_fit的Python非线性回归误差

使用curve_fit的Python非线性回归误差

慕后森 2021-09-14 16:31:20
我正在尝试使用scipy.optimize.curve_fit. 我从此处找到的 Scipy 文档中获取示例代码:https : //docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html我使用简单的数据并绘制它:import numpy as npimport matplotlib.pyplot as pltfrom scipy.optimize import curve_fitxdata = np.array([4.2, 8.5, 10.3, 17.2, 20.7, 38.2, 75.6, 850, 1550])     ydata = np.array([83.3, 53.3, 44.8, 32.6, 28.1, 19.5, 11.5, 5.7, 5.3]) plt.plot(xdata, ydata, 'b-', label='data')这是函数和代码的其余部分:def func(x, a, b, c):    return x*(a*(1-m.exp(-b/x))+c*m.exp(-b/x))-x*cpopt, pcov = curve_fit(func, xdata, ydata)poptplt.plot(xdata, func(xdata, *popt), 'r-',         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))poptplt.plot(xdata, func(xdata, *popt), 'g--',         label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))plt.xlabel('x')plt.ylabel('y')plt.legend()plt.show()我收到以下错误。TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'在所有错误细节之后:error: Result from function call is not a proper array of floats.我试过了 xdata = np.array( ... , dtype='float64'),并尝试了这个线程上提出的所有解决方案,但没有成功:无法将数组数据从 dtype('O') 转换为 dtype('float64')有什么建议和想法可以使这个回归工作吗?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 673 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信