我正在尝试使用scipy.optimize.curve_fit. 我从此处找到的 Scipy 文档中获取示例代码:https : //docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
我使用简单的数据并绘制它:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
xdata = 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*c
popt, pcov = curve_fit(func, xdata, ydata)
popt
plt.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]))
popt
plt.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')
有什么建议和想法可以使这个回归工作吗?
相关分类