开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜