Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the cumulative sum of numpy array in-place

I want to compute the integral image. for example

a=array([(1,2,3),(4,5,6)])
b = a.cumsum(axis=0)

This will generate another array b.Can I execute the cumsum in-place. If not . Are there any other methods to do that

like image 700
Samuel Avatar asked Aug 28 '13 13:08

Samuel


People also ask

What is Cumsum () in Python?

Python NumPy cumsum() function is used to return the cumulative sum of the array elements along the given axis.

How do you do a cumulative sum in a DataFrame in Python?

The cumsum() method returns a DataFrame with the cumulative sum for each row. The cumsum() method goes through the values in the DataFrame, from the top, row by row, adding the values with the value from the previous row, ending up with a DataFrame where the last row contains the sum of all values for each column.

What does NumPy Cumsum return?

cumsum. Return the cumulative sumcumulative sumA running total is the summation of a sequence of numbers which is updated each time a new number is added to the sequence, by adding the value of the new number to the previous running total. Another term for it is partial sum.https://en.wikipedia.org › wiki › Running_totalRunning total - Wikipedia of the elements along a given axis.


1 Answers

You have to pass the argument out:

np.cumsum(a, axis=1, out=a)

OBS: your array is actually a 2-D array, so you can use axis=0 to sum along the rows and axis=1 to sum along the columns.

like image 111
Saullo G. P. Castro Avatar answered Nov 15 '22 22:11

Saullo G. P. Castro