I need to implement a function to sum the elements of an array with a variable section length. Thus,
a = np.arange(10) section_lengths = np.array([3, 2, 4]) out = accumulate(a, section_lengths) print out array([ 3., 7., 35.])
I tried to implement the implementation in cython here:
https://gist.github.com/2784725
for performance I compare with a pure numpy solution for the case when the section lengths are all the same:
LEN = 10000 b = np.ones(LEN, dtype=np.int) * 2000 a = np.arange(np.sum(b), dtype=np.double) out = np.zeros(LEN, dtype=np.double) %timeit np.sum(a.reshape(-1,2000), axis=1) 10 loops, best of 3: 25.1 ms per loop %timeit accumulate.accumulate(a, b, out) 10 loops, best of 3: 64.6 ms per loop
Do you have suggestions for improving productivity?
Andrea Zonca
source share