按索引对numpy数组的累积求和 [英] Cumulative summation of a numpy array by index

查看:25
本文介绍了按索引对numpy数组的累积求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个需要相加的值数组

Assume you have an array of values that will need to be summed together

d = [1,1,1,1,1]

第二个数组指定哪些元素需要相加

and a second array specifying which elements need to be summed together

i = [0,0,1,2,2]

结果将存储在大小为 max(i)+1 的新数组中.因此,例如 i=[0,0,0,0,0] 相当于将 d 的所有元素相加并将结果存储在位置 0 的大小为 1 的新数组.

The result will be stored in a new array of size max(i)+1. So for example i=[0,0,0,0,0] would be equivalent to summing all the elements of d and storing the result at position 0 of a new array of size 1.

我尝试使用

c = zeros(max(i)+1)
c[i] += d

但是,+= 操作只将每个元素添加一次,从而给出了

However, the += operation adds each element only once, thus giving the unexpected result of

[1,1,1]

而不是

[2,1,2]

如何正确实现这种求和?

How would one correctly implement this kind of summation?

推荐答案

这个解决方案对于大型数组应该更有效(它迭代可能的索引值而不是 i 的单个条目):

This solution should be more efficient for large arrays (it iterates over the possible index values instead of the individual entries of i):

import numpy as np

i = np.array([0,0,1,2,2])
d = np.array([0,1,2,3,4])

i_max = i.max()
c = np.empty(i_max+1)
for j in range(i_max+1):
    c[j] = d[i==j].sum()

print c
[1. 2. 7.]

这篇关于按索引对numpy数组的累积求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆