【发布时间】:2020-08-12 00:28:04
【问题描述】:
我正在尝试计算以下数组的两个元素之间的二项式系数:
popul_num = np.array([100, 200, 0, 0]) --> #The discrete number of entities in each reaction
LHS = np.array([[1,1,0,0], [0,0,1,0], [0,0,1,0]]) --> #three rows for 3 different reactions, each row has 4 elements describing the ratio of reactants in that particular reaction
stoch_rate = np.array([0.0016, 0.0001, 0.1000])
我的系统有 4 个实体;酶、底物、酶-底物复合物和产物。其由 popul_num 中的离散反应物编号和 LHS 矩阵中反应物之间的比率描述,按此顺序。
应使用每个实体的离散分子数及其在 LHS 中给出的特定反应中其他实体之间的比率来计算二项式系数。这将导致该反应的倾向函数(见下文)
前几天我收到索引错误时问了一个问题:index 3 is out of bounds for axis 0 with size 3,但通过确保我引用的是索引而不是值来解决这个问题。
但在我的代码版本中,我使用矩阵而不是数组,我已经意识到 popul_num 矩阵的长度是 1,所以我总是 0,我的二项式系数计算不起作用。因此,我将矩阵转换为数组,旨在将 popul_num 数组展平为长度为 4 的一维数组 -->(不确定这是否是最好的方法,但欢迎提出任何其他建议)
但是我又收到了index 3 is out of bounds for axis 0 with size 3 错误,我不知道为什么? python for循环引用矩阵和数组中的值或索引的方式有什么不同吗?
这是我目前得到的代码:
for j in range(len(LHS)):
cj = stoch_rate
for i in range(len(popul_num)):
if i >= LHS[i,j]:
binon_rxn = binom(i, LHS[i, j]) # binomial coefficent isnt working! Always returns 1!
print(binon_rxn)
aj = cj*binon_rxn
else:
cj == 0
aj = (cj*binom(i, LHS[i, j]))
print("Propensity function per reaction:\n", aj)
【问题讨论】:
-
在您的情况下,
stoch_rate的值是多少?还有,binom怎么计算? -
对不起,我已经编辑了帖子,但在这里它们也是 stoch_rate = np.array([0.0016, 0.0001, 0.1000]),我正在使用内置 binom 计算二项式系数,它我认为来自 scipy,但我也导入了 numpy(我在 python 3.7 btw 上)
-
for i in range(len(popul_num)):其中popul_num的长度为4。binon_rxn = binom(i, LHS[i, j])将在i==3时通过该错误,因为LHS 的长度仅为3。 -
我该如何用正确的语法来写对不起?
标签: python arrays numpy indexing