 # Numpy cumsum Implementation in Python with Examples

Numpy cumsum is a method that lets you find the cumulative sum of the data points or array over a given axis. In this entire post, I will discuss its syntax and how to apply it with great examples.

## Syntax and Parameters Explanation

``numpy.cumsum(a, axis=None, dtype=None, out=None)``

Parameter :

a: Input array.

axis: Axis along which you want to find the cumulative sum.

dtype: Type of the array you want to return.

out: Location to output the results. Default is none.

## Examples for the implementation of Numpy Cumsum().

### Example 1: Finding the Numpy cumsum of a Single Dimension Array.

Lets  create a single dimensional array for implementing this example. You can create numpy array using the numpy.array() method.

``array_1d = np.array([10,20,30,40,50,60,70])``

To find the cumulutative sum put the array_1d inside the np.array() method.

``np.cumsum(array_1d)``

After executing the code You will get the following output.

### Example 2: Finding the cumulative sum of a Multi Dimension Array.

In order to find the cumulutaive sum of 2d array you have to take care of the axis argument also.

Without it the sum will returned according to the method 1. Execute the code below to find the sum.

``array_3x4 = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,120]])``

Without axis

``np.cumsum(array_3x4)``

With axis =0

If you will use axis =0 , then cumulative sum will be done vertically or column-wise.

``np.cumsum(array_3x4,axis=0)``

With axis =1

And if the axis is 1, then sum will be done horizontally or row-wise.

``np.cumsum(array_3x4,axis=0)``

### Example 3: Finding the cumulative sum and output it into another array.

In this example I am first finding the sum of a two dimensional array. Then after I will output the results to empty array. It can done by passing the out argument inside the cumsum() method.

I am taking the same 3×4 dimension array. Execute the code below to get the results.

``````sum = np.cumsum(array_3x4,axis=0)
out = np.zeros_like(sum)
np.cumsum(array_3x4,axis=0,out=out)``````

Here I am first assigning sum to memory using  the sum variable. Then I am creating zeros numpy array (out) of the same dimension as the sum variable. Finally calculating the results and outputing it the out variable . The use of out won’t allocate or free any memory, which can save you a lot of time.

Output

### Example 4: Finding the cumulative sum on CSV Data

You can also find the cumulative sum on any CSV data. For example I have CSV data containing marks of each student. My task is to find  the cumumlative sum of the marks columns.

The first thing I will do is to read the csv file using pandas and then finding the cumulative sum of marks after choosing the Marks Column of the dataframe.

Execute the following code to get the results.

``````data = pd.read_csv("strud_array.csv")
marks = np.array(data["Marks"])
np.cumsum(marks)``````

Below is the output you will get.

## End Notes

These are the method  We have aggregated for you. You may use numpy cumsum over financial markets datasets like stock market to find the cumulative returns. Hope you have find this article valuable.  You can contact us to ask for any query. Meanwhile you can give your valuable comment.

Source:

Offical Numpy Documentation 