python - 有谁知道为什么我的函数在另一个函数中调用它时会转换为类型元组?
问题描述
我编写了以下函数来计算系统触发中单个反应的概率,并将系统触发中每个反应的概率输出为数组:
def propensity_calc(LHS, popul_num, stoch_rate):
propensity = np.zeros(len(LHS))
for row in range(len(LHS)):
a = stoch_rate[row]
for i in range(len(popul_num)):
if (popul_num[i] >= LHS[row, i]):
binom_rxn = binom(popul_num[i], LHS[row, i])
a = a*binom_rxn
else:
a = 0
break
propensity[row] = a
return propensity
此函数的输入是 3 个数组,每个数组具有popul_num
每个反应物的离散分子数,LHS
是一个 2D 数组,其中包含每个反应物之间的比率,并且stoch_rate
是每个反应的速率。
我现在想在不同的函数中使用 scipy.misc.derivative 方法调用该函数来计算偏导数:
def time_step_calc(popul_num, state_change_array, a0):
# equation 22:
expctd_net_change = a0*state_change_array
print("expected net change:\n", expctd_net_change)
# equation 24 partial differentiation:
for x in range(len(popul_num)):
part_propensity_diff = derivative(func=propensity_calc(LHS, popul_num, stoch_rate), x0=popul_num[x]) <-- Error here, propensity_calc is converted into a tuple object?
print("partial diff:\n", type(part_propensity_diff))
# popul_num = numpy.ndarray --> derivative function needs a float to work
# equation 26:
t_step = epsi*a0 / sum(expctd_net_change*part_propensity_diff)
delta_t = optimize.fmin(t_step, 0.00012) # take the minimum value of the partial derivative
print("calculated delta_t:\n", delta_t)
return delta_t
但是当我使用 propensity_calc 函数提供函数参数时,指示的行会引发以下错误TypeError: 'tuple' object is not callable
我不明白元组是从哪里来的,或者为什么 propensity_calc 被转换成它?
编辑:继承人回溯
File "C:\Users\Mike\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scipy\misc\common.py", line 119, in derivative
val += weights[k]*func(x0+(k-ho)*dx,*args)
TypeError: 'tuple' object is not callable
有什么建议么?干杯
解决方案
当你说:
func=propensity_calc(LHS, popul_num, stoch_rate)
我不相信你需要
func=
propensity_calc(LHS, popul_num, stoch_rate)
调用 propensity_calc
func=propensity_calc(LHS, popul_num, stoch_rate)
定义一个元组,或不可变的对象序列。想一想您无法更改的对象数组。
这里有更多关于元组的信息:https ://www.tutorialspoint.com/python/python_tuples.htm
推荐阅读
- ios - 上面的黑条 iPhone X。如何摆脱?
- node.js - 如何使用 Puppeteer 登录?错误:找不到选择器的节点:#login-email
- jsf - 不从 JSF 调用包含 Method 的 Lambda
- excel - 在 Power Query 中将负文本转换为十进制数
- angular - [value] 属性中有多个值
- c# - 如何从 C++/WinRT 引发 WinRT/.net 异常?
- routes - Codeigniter 4 路由似乎无法正常工作
- python - 使用 Mailjet API 和 Python 将内联图像插入电子邮件
- apache-spark - 如何根据索引从另一个数据集更新数据集行
- vue.js - 如何使用 cypress 正确测试 vuetify 表单验证错误?