Background
+1 occurs only once for each element although each indexed element is referenced multiple times.
a = np.arange(12).reshape((3, 4))
b = a.copy()
print(a)
---
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
a[1, [0, 2, 0, 2, 0]] += 1 # a[1][0] referenced 3 times and a[1][2] 2 times
print(a-b)
---
[[0 0 0 0]
[1 0 1 0] # <--- +1 occurs only once for a[1][0] and a[1][2]
[0 0 0 0]]
Question
Is there a way to create a sort of accumulative addition like below in a numpy native way?
# Result a
[[ 0 1 2 3]
[ 7 5 8 7] <--- +1 three times on a[1][0] and twice on a[1][2]
[ 8 9 10 11]]
# a-b
[[0 0 0 0]
[3 0 2 0] # <--- +3 for a[1][0] and +2 for a[1][2]
[0 0 0 0]]
One way can be reduce-like, similar to map/reduce word counting, but would like to know if there is a native numpy way.
