当索引重叠时,numpy高级索引就地增量的含义是什么? [英] What are the semantics of numpy advanced indexing in-place increments when the indices overlap?
本文介绍了当索引重叠时,numpy高级索引就地增量的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用高级索引来增加一个numpy数组,例如
I want to increment a numpy array using advanced indexing, e.g.
import numpy
x = numpy.array([0,0])
indices = numpy.array([1,1])
x[indices] += [1,2]
print x #prints [0 2]
我本来希望结果是[0 3],因为应该将1和2都添加到x的第二个零,但是显然numpy仅添加与特定索引匹配的最后一个元素. 这是一般的行为吗,我可以依靠它,还是这种不确定的行为,并且可以使用不同版本的numpy更改?
I would have expected, that the result is [0 3], since both 1 and 2 should be added to the second zero of x, but apparently numpy only adds the last element which matches to a particular index. Is this the general behaviour and I can rely on that, or is this undefined behaviour and could change with a different version of numpy?
此外,是否有一种(简单的)方法可以使numpy添加与索引匹配的所有元素,而不仅仅是最后一个?
Additionally, is there an (easy) way to get numpy to add all elements which match the index and not just the last one?
推荐答案
查看全文