“'numpy.float64' 对象不可调用”- 在函数中使用带有多个参数的 scipy.optimize curve_fit 时出错
"'numpy.float64' object is not callable"-Error when using scipy.optimize curve_fit with multiple parameters in function
我是 python 和编程的新手。如果之前有人回答过这个问题,请提前致歉,我已经检查了我能找到的所有内容,但我无法解决它。
我正在尝试逼近一个 S 型函数(用于创新扩散预测),准确地说是 Bass 模型,具有有限数量的值(历史和未来预测)。
使用的函数在下面的代码中
变量含义如下:y:市场扩散0≤y≤1,t:年数
参数含义如下:m: Maximum Maket Size, p: early adopters/innovators, q: immitators
现在,我收到以下错误:'numpy.float64' object is not callable
我的代码需要更改什么才能正常运行?
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
from scipy.optimize import curve_fit
import pylab as p
t_data = np.array([2016, 2017, 2018, 2019, 2020, 2025, 2030, 2035, 2040, 2045])
y_data = np.array([0.0015, 0.0027, 0.0042, 0.0066, 0.0164, 0.1389, 0.3889, 0.6667, 0.8611, 0.99])
def bass(t, *args):
m, p, q = args
t_0 = 2011
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
popt, pcov = curve_fit(bass, t_data, y_data, p0=[1, 1, 1])
plt.plot(t_data, y_data, 'o')
plt.plot(t_data, func_nl_lsq(t_data, *popt), '-')
plt.show()
这是我收到的完整错误:
<ipython-input-30-3b4f9481e8a6>:6: RuntimeWarning: overflow encountered in exp
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
<ipython-input-30-3b4f9481e8a6>:6: RuntimeWarning: invalid value encountered in true_divide
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-3b4f9481e8a6> in <module>
7 popt, pcov = curve_fit(bass, t_data, y_data, p0=[1, 1, 1])
8 plt.plot(t_data, y_data, 'o')
----> 9 plt.plot(t_data, func_nl_lsq(t_data, *popt), '-')
10 plt.show()
<ipython-input-17-286e00184588> in func_nl_lsq(t, *args)
4 m, p, q = args
5 t_0 = 2011
----> 6 return m*((1-np.exp(-(p+q)(t-t_0)))/(1+(p/q)*np.exp(-(p+q)(t-t_0))))
7 popt, pcov = curve_fit(func_nl_lsq, t_data, y_data, p0=[1, 1, 1])
8 plt.plot(t_data, y_data, 'o')
TypeError: 'numpy.float64' object is not callable
最后一个错误TypeError: 'numpy.int64' object is not callable
是由于表达式(p+q)(t-t_0)
,它被解释为函数调用。您必须明确写下乘法,即:
def bass(t, *args):
m, p, q = args
t_0 = 2011
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
看起来您还需要在调用 bass
函数时包含参数。就像是:
curve_fit(bass(t_data, args), rest of curve_fit arguments...)
我是 python 和编程的新手。如果之前有人回答过这个问题,请提前致歉,我已经检查了我能找到的所有内容,但我无法解决它。
我正在尝试逼近一个 S 型函数(用于创新扩散预测),准确地说是 Bass 模型,具有有限数量的值(历史和未来预测)。
使用的函数在下面的代码中
变量含义如下:y:市场扩散0≤y≤1,t:年数
参数含义如下:m: Maximum Maket Size, p: early adopters/innovators, q: immitators
现在,我收到以下错误:'numpy.float64' object is not callable
我的代码需要更改什么才能正常运行?
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
from scipy.optimize import curve_fit
import pylab as p
t_data = np.array([2016, 2017, 2018, 2019, 2020, 2025, 2030, 2035, 2040, 2045])
y_data = np.array([0.0015, 0.0027, 0.0042, 0.0066, 0.0164, 0.1389, 0.3889, 0.6667, 0.8611, 0.99])
def bass(t, *args):
m, p, q = args
t_0 = 2011
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
popt, pcov = curve_fit(bass, t_data, y_data, p0=[1, 1, 1])
plt.plot(t_data, y_data, 'o')
plt.plot(t_data, func_nl_lsq(t_data, *popt), '-')
plt.show()
这是我收到的完整错误:
<ipython-input-30-3b4f9481e8a6>:6: RuntimeWarning: overflow encountered in exp
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
<ipython-input-30-3b4f9481e8a6>:6: RuntimeWarning: invalid value encountered in true_divide
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-3b4f9481e8a6> in <module>
7 popt, pcov = curve_fit(bass, t_data, y_data, p0=[1, 1, 1])
8 plt.plot(t_data, y_data, 'o')
----> 9 plt.plot(t_data, func_nl_lsq(t_data, *popt), '-')
10 plt.show()
<ipython-input-17-286e00184588> in func_nl_lsq(t, *args)
4 m, p, q = args
5 t_0 = 2011
----> 6 return m*((1-np.exp(-(p+q)(t-t_0)))/(1+(p/q)*np.exp(-(p+q)(t-t_0))))
7 popt, pcov = curve_fit(func_nl_lsq, t_data, y_data, p0=[1, 1, 1])
8 plt.plot(t_data, y_data, 'o')
TypeError: 'numpy.float64' object is not callable
最后一个错误TypeError: 'numpy.int64' object is not callable
是由于表达式(p+q)(t-t_0)
,它被解释为函数调用。您必须明确写下乘法,即:
def bass(t, *args):
m, p, q = args
t_0 = 2011
return m*(1-np.exp(-(p+q)*(t-t_0)))/(1+(p/q)*np.exp(-(p+q)*(t-t_0)))
看起来您还需要在调用 bass
函数时包含参数。就像是:
curve_fit(bass(t_data, args), rest of curve_fit arguments...)