我对Python很陌生。我想用curve_fit做一个适合我数据的指数。这是我的代码:
import numpy as np
from scipy.optimize import curve_fit
xdata= np.array('1, 8, 8, 21, 31, 42, 63, 64, 81, 110, 156, 211, 301, 336, 735')
ydata = np.array('0.018, 0.0164, 0.0042, 0.0072, 0.0108, 0.0044, 0.0035, 0.0036, 0.0042, 0.0051, 0.0019, 0.0042, 0.0019, 8e-4, 2e-4')
def func(x,a,b,m,n):
return a*np.exp(m*x)+b*np.exp(n*x)
curve_fit(func, xdata, ydata)
我得到了类型错误陈述:" ufunc'乘以'没有包含签名匹配类型dtype的循环('
有人可以帮帮我吗?另外,我想设置一个约束,使参数a和b加到1。
谢谢。
答案 0 :(得分:0)
import numpy as np
from scipy.optimize import curve_fit
xdata= np.array([1, 8, 8, 21, 31, 42, 63, 64, 81, 110, 156, 211, 301, 336, 735])
ydata = np.array([0.018, 0.0164, 0.0042, 0.0072, 0.0108, 0.0044, 0.0035, 0.0036, 0.0042, 0.0051, 0.0019, 0.0042, 0.0019, 8e-4, 2e-4])
def func(x,a,b,m,n):
return a*np.exp(m*x)+b*np.exp(n*x)
curve_fit(func, xdata, ydata)
不幸Covariance of the parameters could not be estimated