Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy sum of values in subarrays between pairs of indices

Tags:

python

numpy

sum

Suppose I have an array A. I have a series of index pairs (a1, b1), (a2, b2) ... (an, bn)

I want to obtain all the sums of the elements between those pairs. i.e.

sum(A[a1:b1]), sum(A[a2:b2]), sum(A[a3:b3]) ...

In terms of run-time, what's the most efficient way of doing this?

Thanks!

like image 811
CuriousMind Avatar asked Dec 27 '22 14:12

CuriousMind


2 Answers

Assuming your index pairs are stored in a NumPy array indices of shape (n, 2) and n is fairly large, it is probably best to avoid any Python loop:

c = numpy.r_[0, A.cumsum()][indices]
sums = c[:,1] - c[:,0]
like image 141
Sven Marnach Avatar answered Dec 30 '22 04:12

Sven Marnach


Here's another way:

a = np.random.rand(3000)
indices = np.array([[0,3], [9,20], [5,30], [9,33]])
sums = np.add.reduceat(a, indices.ravel())[::2]

assert np.all(sums == np.array([a[i:j].sum() for i,j in indices]))

The cumsum one above is probably more efficient if there are many indices.

like image 26
pv. Avatar answered Dec 30 '22 02:12

pv.