[Numpy-discussion] Incrementing with advanced indexing: why don't repeated indexes repeatedly increment?

John Salvatier jsalvati at u.washington.edu
Wed Jun 6 04:48:18 EDT 2012


Hello,

I've noticed that If you try to increment elements of an array with
advanced indexing, repeated indexes don't get repeatedly incremented. For
example:

In [30]: x = zeros(5)

In [31]: idx = array([1,1,1,3,4])

In [32]: x[idx] += [2,4,8,10,30]

In [33]: x
Out[33]: array([  0.,   8.,   0.,  10.,  30.])

I would intuitively expect the output to be array([0,14, 0,10,30]) since
index 1 is incremented by 2+4+8=14, but instead it seems to only increment
by 8. What is numpy actually doing here?

The authors of Theano noticed this behavior a while ago so they python loop
through the values in idx (this kind of calculation is necessary for
calculating gradients), but this is a bit slow for my purposes, so I'd like
to figure out how to get the behavior I expected, but faster.

I'm also not sure how to navigate the numpy codebase, where would I look
for the code responsible for this behavior?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120606/4d23735d/attachment.html>


More information about the NumPy-Discussion mailing list