简体   繁体   中英

Vectorization of cumulative sum in python

I'm trying to vectorize/broadcast (Not sure what it is called formally) my code in order to make it faster, but I can't quite get it. What I think I should be using is numpy.cumsum (with axis=0) but I don't know how to get it (fast) in the correct array to use it.

What I want with this code is basically the absolute sum of l1 for adding each element from l2 to all numbers in l1. So this gives not one answer, but len(l2) amount of answers. The (non-vectorized) code below give the correct output.

    # l1 and l2 are numpy arrays
    for i in l2:
        l1 += i
        answer = numpy.sum(numpy.absolute(l1))
        print answer

Can anyone provide an answer or hint?

The trick is to first combine the two one-dimensional arrays into a single two-dimensional array, and then sum over that. If you have a vector of shape (a,1) and you broadcast it with an array of shape (b,) , the resulting array will be shape (a,b) . It's handy to add extra axes with length one into arrays to get this sort of behavior.

Here's a way to get the same answer without loops

# Assume l1 has length n1, l2 has length n2
suml2 = np.cumsum(l2)  # length n2
y = l1 + suml2[:,np.newaxis]  # shape (n2, n1)
answer = np.sum(np.abs(y), axis=1)  # shape (n2,)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM