Cumulative summation of a numpy array by index Cumulative summation of a numpy array by index numpy numpy

Cumulative summation of a numpy array by index


If I understand the question correctly, there is a fast function for this (as long as the data array is 1d)

>>> i = np.array([0,0,1,2,2])>>> d = np.array([0,1,2,3,4])>>> np.bincount(i, weights=d)array([ 1.,  2.,  7.])

np.bincount returns an array for all integers range(max(i)), even if some counts are zero


Juh_'s comment is the most efficient solution. Here's working code:

import numpy as npimport scipy.ndimage as nii = np.array([0,0,1,2,2])d = np.array([0,1,2,3,4])n_indices = i.max() + 1print ni.sum(d, i, np.arange(n_indices))


This solution should be more efficient for large arrays (it iterates over the possible index values instead of the individual entries of i):

import numpy as npi = np.array([0,0,1,2,2])d = np.array([0,1,2,3,4])i_max = i.max()c = np.empty(i_max+1)for j in range(i_max+1):    c[j] = d[i==j].sum()print c[1. 2. 7.]