We can use np.bincount, which is supposedly quite effective for such a cumulative weighted calculation, so here is one with this -
counts = np.bincount(i,v)
d[:counts.size] = counts
, minlength , d , -
d += np.bincount(i,v,minlength=d.size).astype(d.dtype, copy=False)
np.add.at, other post, np.bincount, .
In [61]: def bincount_based(d,i,v):
...: counts = np.bincount(i,v)
...: d[:counts.size] = counts
...:
...: def add_at_based(d,i,v):
...: np.add.at(d, i, v)
...:
In [62]:
...: N = 10000
...: i = np.random.randint(0,1000,(N))
...: v = np.random.randint(0,1000,(N))
...:
...:
...: M = 12000
...: d1 = np.zeros(M)
...: d2 = np.zeros(M)
...:
In [63]: bincount_based(d1,i,v)
...: add_at_based(d2,i,v)
...:
In [64]: np.allclose(d1,d2)
Out[64]: True
In [67]:
...: M = 12000
...: d1 = np.zeros(M)
...: d2 = np.zeros(M)
...:
In [68]: %timeit add_at_based(d2,i,v)
1000 loops, best of 3: 1.83 ms per loop
In [69]: %timeit bincount_based(d1,i,v)
10000 loops, best of 3: 52.7 µs per loop