Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 1 to numpy array from a list of indices? [duplicate]

Tags:

python

numpy

I have a numpy array-like

x = np.zeros(4, dtype=np.int)

And I have a list of indices like [1, 2, 3, 2, 1] and I want to add 1 to the corresponding array elements, such that for each element in the index list, x is incremented at that position:

x = [0, 2, 2, 1]

I tried doing this using:

x[indices] += 1

But for some reason, it only updates the indices once, and if an index occurs more often than once it is not registered. I could of course just create a simple for loop but I was wondering if there is a one-line solution.

like image 361
Caspertijmen1 Avatar asked Oct 13 '25 00:10

Caspertijmen1


2 Answers

What you are essentially trying to do, is to replace the indexes by their frequencies.

Try np.bincount. Technically that does the same what you are trying to do.

indices = [1, 2, 3, 2, 1]

np.bincount(indices)
array([0, 2, 2, 1])

If you think about what you are doing. You are saying that for index 0, you dont want to count anything. but for index 1, you want 2 counts, .. and so on. Hope that gives you an intuitive sense of why this is the same.

@Stef's solution with np.unique, does exactly the same thing as what np.bincount would do.

like image 91
Akshay Sehgal Avatar answered Oct 14 '25 14:10

Akshay Sehgal


You want np.add.at:

np.add.at(x, indices, 1)
x
Out[]:
array([0, 2, 2, 1])

This works even if x doesn't start out as np.zeros

like image 21
Daniel F Avatar answered Oct 14 '25 13:10

Daniel F