13

I have an array:

A = np.array([0, 0, 0])

and list of indices with repetitions:

idx = [0, 0, 1, 1, 2, 2]

and another array i would like to add to A using indices above:

B = np.array([1, 1, 1, 1, 1, 1])

The operation:

A[idx] += B

Gives the result: array([1, 1, 1]), so obviously values from B were not summed up. What is the best way to get as a result array([2, 2, 2])? Do I have to iterate over indices?

3
  • 1
    related: stackoverflow.com/q/15973827/2096752 and stackoverflow.com/q/16034672/2096752 Commented Jun 7, 2014 at 16:46
  • 1
    The first duplicate is not actually a duplicate, it talks about assignment which is completely different from addition. Commented Jan 15, 2017 at 11:36
  • 1
    I feel also the second duplicate is not an exact duplicate. Please remove this duplication info. Commented Dec 6, 2019 at 8:35

2 Answers 2

30

for this numpy 1.8 added the at reduction:

at(a, indices, b=None)

Performs unbuffered in place operation on operand 'a' for elements specified by 'indices'. For addition ufunc, this method is equivalent to a[indices] += b, except that results are accumulated for elements that are indexed more than once. For example, a[[0,0]] += 1 will only increment the first element once because of buffering, whereas add.at(a, [0,0], 1) will increment the first element twice.

.. versionadded:: 1.8.0

In [1]: A = np.array([0, 0, 0])
In [2]: B = np.array([1, 1, 1, 1, 1, 1])
In [3]: idx = [0, 0, 1, 1, 2, 2]
In [4]: np.add.at(A, idx, B)
In [5]: A
Out[5]: array([2, 2, 2])
Sign up to request clarification or add additional context in comments.

1 Comment

That's it! It seems to be couple of times faster than iterating and works also on multidimensional arrays. Thanks.
1

How about:

A = np.array([1, 2, 3])
idx = [0, 0, 1, 1, 2, 2]
A += np.bincount(idx, minlength=len(A))

Obviously it's even more simple if A starts off as zeros:

A = np.bincount(idx)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.