Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Cumulative sum of 1d NumPy Array

Tags:

python

numpy

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?

like image 966
Idr Avatar asked Apr 27 '11 02:04

Idr


People also ask

How do you compute the sum of all the elements in a numpy array?

sum() in Python. The numpy. sum() function is available in the NumPy package of Python. This function is used to compute the sum of all elements, the sum of each row, and the sum of each column of a given array.

What is a 1d numpy array?

One dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple.


1 Answers

y = np.cumsum(x)

See http://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html

like image 127
Dan D. Avatar answered Oct 15 '22 08:10

Dan D.