按索引对numpy数组的累积求和

Cumulative summation of a numpy array by index(按索引对numpy数组的累积求和)
本文介绍了按索引对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数组的累积求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)