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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reading *.mhd/*.raw format in python(在 python 中读取 *.mhd/*.raw 格式)
Count number of cells in the image(计算图像中的单元格数)
How to detect paragraphs in a text document image for a non-consistent text structure in Python OpenCV(如何在 Python OpenCV 中检测文本文档图像中的段落是否存在不一致的文本结构)
How to get the coordinates of the bounding box in YOLO object detection?(YOLO物体检测中如何获取边界框的坐标?)
Divide an image into 5x5 blocks in python and compute histogram for each block(在 python 中将图像划分为 5x5 块并计算每个块的直方图)
Extract cow number from image(从图像中提取奶牛编号)