简体   繁体   English

当一个元素由公式派生时,如何对两个 arrays 进行元素乘法?

[英]How can I do element-wise multiplication of two arrays when one element is derived by a formula?

I'm trying to multiply two arrays together element wise我正在尝试将两个 arrays 元素相乘

expected_state = np.array([-1.004  0.002  0.0]) 
b = np.array([[1.0, 0.0, 0.0], [[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])

Each element of expected_state should be multiplied postionally with every element from each row in b So [[-1.004*1.0, 0.002*0.0, 0.0*0.0], [-1.004*[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.002*0.0....etc]] expected_state的每个元素都应该与b中每一行的每个元素按位置相乘 所以[[-1.004*1.0, 0.002*0.0, 0.0*0.0], [-1.004*[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.002*0.0....etc]]

Array b is defined in a function so that the first element in row two can change as stoch_rate and popul_num change as the program executes.数组b在 function 中定义,因此第二行的第一个元素可以随着程序执行而随着stoch_ratepopul_num的变化而变化。

def update_matrix(popul_num, stoch_rate): 
    """Specific to this model 
    will need to change if different model 
    implements equaiton 24 of the Gillespie paper"""
    b = np.array([[1.0, 0.0, 0.0], [[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.0, 0.0], [0.0, 0.5, 
0.0], [0.0, 0.4, 0.0]])
    return b

So far I've used nested for loops to try and do the multiplication:到目前为止,我已经使用嵌套的 for 循环来尝试进行乘法运算:

for j in range(len(evaluate_propensity)):
    for i in range(len(popul_num)):
        denominator[j] += (exptd_state_array[i]*b[j, i]) # TypeError: Cant multiply sequence by non-int type "numpy.float64"

But get the TypeError: can't multiply sequence by non-int of type 'numpy.float64'但是得到 TypeError: can't multiply sequence by non-int of type 'numpy.float64'

I've had a look at some other posts which say things like this happen when trying to multiply list indecies with non-integers because list indecies can't have partial numbers.我看过其他一些帖子,它们说在尝试将列表不定数与非整数相乘时会发生这种情况,因为列表不定数不能有部分数字。 Which I understand but the elements of my arrays are meant to be floats so I'm not too sure how to over come that.我明白,但我的 arrays 的元素是浮点数,所以我不太确定如何克服它。

Then after reading some more, I found that the error came when the program was trying to do the multiplication of the [stoch_rate[1]*(2*(popul_num[0]) - 1)/2] element from array b and was wondering if the TypeError would come from that formula derived element of the array and if it does how could that be fixed?然后在阅读了更多内容后,我发现当程序试图对数组b中的[stoch_rate[1]*(2*(popul_num[0]) - 1)/2]元素进行乘法运算时出现错误并且是想知道TypeError是否来自该数组的公式派生元素,如果确实如此,如何解决?

Cheers干杯

EDIT:编辑:

popul_num = np.array([1.0E5, 0, 0]) # array of molecule numbers for 3 species in model
stoch_rate = np.array([1.0, 0.002, 0.5, 0.04]) # rates of the 4 reactions in the model 
evaluate_propensity = np.array(a, b, c, d) # An array of the probability of each  reaction occuring, is dynamically calculated on each iteration so isn't hard coded.  

exptd_state_array and expected_state are the same thing sorry forgot to change the short hand
popul_num = np.array([1.0E5, 0, 0]) # array of molecule numbers for 3 species in model
stoch_rate = np.array([1.0, 0.002, 0.5, 0.04]) # rates of the 4 reactions in the model 
evaluate_propensity = np.array((.25,.25,.25,.25)) # An array of the probability of each  reaction occuring, is dynamically calculated on each iteration so isn't hard coded.  
denominator = np.zeros(4,)

expected_state = np.array([-1.004,  0.002,  0.0]) 
exptd_state_array = expected_state
b = np.array([[1.0, 0.0, 0.0], [[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])

b

array([[1.0, 0.0, 0.0],
       [list([199.999]), 0.0, 0.0],
       [0.0, 0.5, 0.0],
       [0.0, 0.4, 0.0]], dtype=object)

so, b has mixed types.所以, b 有混合类型。 The list is generated by the square brackets around [stoch_rate[1]*(2*(popul_num[0]) - 1)/2]该列表由[stoch_rate[1]*(2*(popul_num[0]) - 1)/2]周围的方括号生成

Multiplication for lists is defined as concatenation with itself: 3 * [5] = [5, 5, 5] .列表的乘法被定义为与自身的串联: 3 * [5] = [5, 5, 5] This fails with floats, as @hpaulj pointed out in the comment.正如@hpaulj 在评论中指出的那样,这会因浮动而失败。

leaving out the square brackets:省略方括号:

b = np.array([[1.0, 0.0, 0.0], [stoch_rate[1]*(2*(popul_num[0]) - 1)/2, 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])

b 


array([[  1.   ,   0.   ,   0.   ],
       [199.999,   0.   ,   0.   ],
       [  0.   ,   0.5  ,   0.   ],
       [  0.   ,   0.4  ,   0.   ]])

Then, the double loop does execute然后,双循环确实执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM