Perform a reverse cumulative sum on a numpy array

前端 未结 3 1258
野的像风
野的像风 2021-01-07 16:18

Can anyone recommend a way to do a reverse cumulative sum on a numpy array?

Where \'reverse cumulative sum\' is defined as below (I welcome any corrections on the na

3条回答
  •  不要未来只要你来
    2021-01-07 16:43

    The answers given so far seem to be all inefficient if you want the result stored in the original array. As well, if you want a copy, keep in mind this will return a view not a contiguous array and np.tocontiguousarray() is still needed.

    How about

    view=np.flip(x, 0)
    np.cumsum(view, 0, out=view)
    #x contains the reverse cumsum result and remains contiguous and unflipped
    

    This modifies the flipped view of x which writes the data properly in reverse order back into the original x variable. It requires no non-contiguous views at the end of execution and is about as speed efficient as possible. I am guessing numpy will never add a reversecumsum method namely because the technique I describe is so trivially and efficiently possible. Albeit, it might be ever so slightly more efficient to have the explicit method.

    Otherwise if a copy is desired, then the extra flip is required AND conversion back to a contiguous array, mainly if it will be used in many vector operations thereafter. A tricky part of numpy, but views and contiguity are something to be careful with if you are seriously interested in performance.

提交回复
热议问题