我正在尝试将曲线拟合到某些点。
### Analysis: cost function
md = 215 / 0.89
wl = [0, 0.5, 1, 1.5, 2, 3, 4, 5, 6]
d = [0, 0.49, 0.71, 0.84, 0.95, 0.98, 1.0, 1.0, 1.0]
dr = []
for i in d: dr.append(i*md)
f, ax = plt.subplots(figsize=(9.5, 6.5))
ax = setFont(ax, 'Arial', 14)
ax.plot(wl, dr, lw=2)
grid()
这是典型的物流功能。这就是我在做的
from scipy.optimize import curve_fit
def func(t,alpha, a):
return 241.573 / 1+ (a * np.exp(alpha * t))
# coefficients and curve fit for curve
popt, pcov = curve_fit(func, wl, dr)
alpha, a = popt
v_fit = func(wl, alpha, a)
但是我得到了错误
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
答案 0 :(得分:1)
该错误是由于wl不是numpy数组而引起的:
import numpy as np
from scipy.optimize import curve_fit
md = 215 / 0.89
wl = np.array([0, 0.5, 1, 1.5, 2, 3, 4, 5, 6])
d = np.array([0, 0.49, 0.71, 0.84, 0.95, 0.98, 1.0, 1.0, 1.0])
dr = np.array([i*md for i in d])
def func(t,alpha, a):
return 241.573 / 1+ (a * np.exp(alpha * t))
# coefficients and curve fit for curve
popt, pcov = curve_fit(func, wl, dr)
alpha, a = popt
v_fit = func(wl, alpha, a)
答案 1 :(得分:0)
基于发现的示例代码here:
from scipy.optimize import curve_fit
import numpy as np
# assume func is defined to calculate your y-values and has the signature func(x, a, b, c)
xdata = np.linspace(0,4,50)
np.random.seed(1729)
ydata = [func(x, 2.5, 1.3, 0.5)+(0.2*np.random.normal(size=xdata.size)) for x in xdata]
plt.plot(xdata, ydata, 'b-', label='data')