【问题标题】:Python Index 3 is out of bounds for axis 0 with size 3对于大小为 3 的轴 0,Python 索引 3 超出范围
【发布时间】: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


【解决方案1】:

问题出在两个 for 循环的迭代器中。第一个迭代器应该是i 而不是j,第二个应该是j 而不是i

import numpy as np
from scipy.special import binom


popul_num = np.array([100, 200, 0, 0])
LHS = np.array([[1,1,0,0], [0,0,1,0], [0,0,1,0]])
stoch_rate = np.array([0.0016, 0.0001, 0.1000])

for i in range(len(LHS)):  #change this to `i`
    cj = stoch_rate
    for j in range(len(popul_num)): # change this to `j`
        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)

得到这个输出:

1.0
1.0
Propensity function per reaction:
 [0.0016 0.0001 0.1   ]
1.0
1.0
1.0
1.0
Propensity function per reaction:
 [0.0016 0.0001 0.1   ]
1.0
1.0
2.0
1.0
Propensity function per reaction:
 [0.0016 0.0001 0.1   ]

【讨论】:

  • 好的,在这种情况下,我应该在二项式系数计算中也将 [i, j] 换成 [j, i] 吗?因为我仍然认为这些位不能正常工作,所以如果我改变 popul_num 中它不 atm 的离散分子数,二项式的输出应该会改变
  • 我不知道您的代码背后的想法,以便告诉您为什么binon_rxn 没有返回预期值。你使用某个教程吗?
  • 这是我正在尝试实现的链接:github.com/karinsasaki/gillespie-algorithm-python/blob/master/…,但这是在 python 2.7 中,所以我不得不调整它的一部分并忽略其他部分等,我认为最当他们使用矩阵/数组自动化时,有用的位在底部
  • 很抱歉,我很难跟上这一切!
  • 别担心!感谢您的尝试:)
猜你喜欢
  • 2019-08-26
  • 2018-07-23
  • 1970-01-01
  • 2018-06-30
  • 2015-05-26
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2018-02-12
相关资源
最近更新 更多