[英]Python numpy : Sum an array selectively given information from a second array
Let's say I have a N-dimensionnal array, for example:假设我有一个 N 维数组,例如:
A = [ [ 1, 2] ,
[6, 10] ]
and another array B that defines an index associated with each value of A另一个数组 B 定义了与 A 的每个值相关联的索引
B = [[0, 1], [1, 0]]
And I want to obtain a 1D list or array that for each index contains the sum of the values of A associated with that index.我想获得一个一维列表或数组,其中每个索引都包含与该索引关联的 A 的值的总和。 For our example, we would want
对于我们的示例,我们希望
C = [11, 8]
Is there a way to do this efficiently, without looping over the arrays manually ?有没有办法有效地做到这一点,而无需手动循环遍历数组?
Edit: To make it clearer what I want, if we now take A the same and B equal to :编辑:为了更清楚地说明我想要什么,如果我们现在取 A 相同而 B 等于:
B = [[1, 1], [1,1]] B = [[1, 1], [1,1]]
Then I want all the values of A to sum into the index 1 of C, which yields然后我希望 A 的所有值总和为 C 的索引 1,从而产生
C = [0, 19] C = [0, 19]
Or I can write a code snippet :或者我可以写一个代码片段:
C = np.zeros(np.max(B))
for i in range(...):
for j in range(...):
C[B[i,j]] += A[i,j]
return C
For each index, or rather tag , you first get a True
/ False
array of B
where it's equal to the tag.对于每个索引,或者更确切地说tag ,你首先得到一个
B
的True
/ False
数组,它等于标签。 Then you use numpy.nonzero
to get indices by dimension of where the True
s are in that array.然后,您使用
numpy.nonzero
按True
s在该数组中的位置的维度获取索引。 When you index A
with these indices, you get a new array with only the tagged elements of A
.当您使用这些索引对
A
进行索引时,您将获得一个仅包含A
标记元素的新数组。 Finally you can sum
over it.最后你可以
sum
一下。
[np.sum(A[np.nonzero(B==tag)]) for tag in [0,1]]
You could have also just done np.sum(A*(B==tag))
because the False
s multiply as 0s, but then you'd be summing over a lot of 0s.你也可以只做
np.sum(A*(B==tag))
因为False
s乘以0,但是你会总结很多0。
I think I found the best answer for now actually.我想我现在找到了最好的答案。 I can just use np.histogram(B, weights = A) which should do the operation I want.
我可以只使用 np.histogram(B, weights = A) 这应该做我想要的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.