I have a (large) array of data and a (large) list of lists of (multiple) indexes, e.g.
data = [1.0, 10.0, 100.0]
contribs = [[1, 2], [0], [0, 1]]
For each entry in, contribsI would like to summarize the corresponding values dataand put them in an array. In the above example, the expected result would be
out = [110.0, 1.0, 11.0]
Doing this in a loop works,
c = numpy.zeros(len(contribs))
for k, indices in enumerate(contribs):
for idx in indices:
c[k] += data[idx]
but since datathey are contribslarge, it takes too much time.
I have a feeling that this can be improved using numpy fancy indexing.
Any clues?
source
share