Running Cumulative sum of 1d NumPy Array
I have a numpy array like
x=np.array([1,2,3,4])
I want to create another numpy array y which is the cumulative sum 开发者_开发问答of x, so that
y=np.array([1,3,6,10])
What is a good num-Pythonic way to do this?
y = np.cumsum(x)
See http://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html
Another option is:
y = np.add.accumulate(x)
which is often times faster than np.cumsum
even though the documentation says they are equivalent.
精彩评论