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
You can use .flipud()
for this as well, which is equivalent to [::-1]
https://docs.scipy.org/doc/numpy/reference/generated/numpy.flipud.html
In [0]: x = np.array([0,1,2,3,4])
In [1]: np.flipud(np.flipud(x).cumsum())
Out[1]: array([10, 10, 9, 7, 4]
.flip()
is new as of NumPy 1.12, and combines the .flipud()
and .fliplr()
into one API.
https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html
This is equivalent, and has fewer function calls:
np.flip(np.flip(x, 0).cumsum(), 0)