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?
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.
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.
y = np.cumsum(x)
See http://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With