0

Given a list of numpy arrays, each of different length, as that obtained by doing lst = np.array_split(arr, indices), how do I get the sum of every array in the list? (I know how to do it using list-comprehension but I was hoping there was a pure-numpy way to do it).

I thought that this would work:

np.apply_along_axis(lambda arr: arr.sum(), axis=0, arr=lst)

But it doesn't, instead it gives me this error which I don't understand:

ValueError: operands could not be broadcast together with shapes (0,) (12,)

NB: It's an array of sympy objects.

1
  • Please see if this works : [sum(x) for x in lst] Commented Jul 8, 2021 at 10:22

1 Answer 1

2

There's a faster way which avoids np.split, and utilizes np.reduceat. We create an ascending array of indices where you want to sum elements with np.append([0], np.cumsum(indices)[:-1]). For proper indexing we need to put a zero in front (and discard the last element, if it covers the full range of the original array.. otherwise just delete the [:-1] indexing). Then we use the np.add ufunc with np.reduceat:

import numpy as np
arr = np.arange(1, 11)
indices = np.array([2, 4, 4])

# this should split like this
# [1  2 | 3  4  5  6 | 7  8  9  10]

np.add.reduceat(arr, np.append([0], np.cumsum(indices)[:-1]))
# array([ 3, 18, 34])
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thx! that's a very handy feature I didn't know about. One remark though: the indices for array_split are not the chunk-lengths but the splitting indices, so you don't need the np.cumsum(arr)[:-1] part. And also coincidentally my indices come from doing _, indices = np.unique(arr, return_index=True) on a different array, so the 0 at the front is already present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.