## Introduction to NumPy cumsum

Whenever we want to calculate the cumulative sum of the elements present in a given array, as in the second elements in the array is the sum of first element and second element, the third element in the array is the sum of first element, second element and third element and so on. We make use of a function called cumsum in numpy which takes four parameters namely arrayname, axis, datatype and output where arrayname represents the name of the array whose cumulative sum of the elements must be calculated, axis represents the axis along which the cumulative sum of the elements must be calculated and if this parameter is not defined, the given array is flattened and then the cumulative sum of the elements will be calculated, datatype is the datatype of the elements in the output array represented by output which is optional.

**Syntax:**

`numpy.cumsum(arrayname, axis, datatype, output)`

Where,

- arrayname is the name of the array whose cumulative sum of the elements must be found.
- axis represents the axis along which the cumulative sum of the elements must be calculated and if this parameter is not defined, the given array is flattened and then the cumulative sum of the elements will be calculated.
- datatype is the datatype of the values resulting in the output array.
- output represents the output array which stores the cumulative sum of the elements from the given input array represented by arrayname and this parameter is optional.

### Working of NumPy cumsum Function

- Whenever we want to calculate the cumulative sum of the elements present in a given array, as in the second elements in the array is the sum of first element and second element, the third element in the array is the sum of first element, second element and third element and so on, we make use of a function called cumsum in numpy.
- Axis represents the axis along which the cumulative sum of the elements must be calculated and if this parameter is not defined, the given array is flattened and then the cumulative sum of the elements will be calculated.
- arrayname represents the name of the array whose cumulative sum of the elements must be calculated.
- datatype is the datatype of the values resulting in the output array.
- Output represents the output array which stores the cumulative sum of the elements from the given input array represented by arrayname and this parameter is optional.

### Examples of NumPy cumsum

Given below are the examples :

#### Example #1

Python program to demonstrate NumPy cumsum function to create an array using array function in numpy and then using cumsum function to find the cumulative sum of the elements of the given array and represent it in an output array.

**Code:**

`#importing the package numpy`

import numpy as nump

#Creating an array by making use of array function in NumPy and storing it in a variable called initialarray

initialarray = nump.array([[8,9],[2,3]])

#Displaying the elements of intialarray followed by one line space by making use of \n

print 'The elements of the given array are:'

print initialarray

print '\n'

#using cumsum function of NumPy and passing the created array as the parameter to that function to find the cumulative sum of all the elements in the array and store it in a variable called cumsumarray

cumsumarray = nump.cumsum(initialarray)

#Displaying the array consisting of cumulative sum of all the elements in the array

print 'The array consisting of cumulative sum of all the elements of the given array using cumsum function is as follows:'

print cumsumarray

**Output:**

In the above program, a package called numpy is imported which allows us to make use of functions called array and cumsum. Then an array called initialarray is created by making use of array function. Then the elements of initialarray are displayed on the screen. Then cumsum function is used to find the cumulative sum of all the elements in initialarray and stored in a variable called cumsumarray. Then the cumsumarray consisting of cumulative sum of all the elements is displayed on the screen.

#### Example #2

Python program to demonstrate NumPy cumsum function to create an array using array function in numpy and then using cumsum function to find the cumulative sum of the elements of the given array along the axis=0 and along the axis=1 and represent it in an output array.

**Code:**

`#importing the package numpy`

import numpy as nump

#Creating an array by making use of array function in NumPy and storing it in a variable called initialarray

initialarray = nump.array([[8,9],[2,3]])

#Displaying the elements of intialarray followed by one line space by making use of \n

print 'The elements of the given array are:'

print initialarray

print '\n'

#using cumsum function of NumPy and passing the created array as the parameter to that function to find the cumulative sum of all the elements in the array and store it in a variable called cumsumarray

cumsumarrayalongaxis0 = nump.cumsum(initialarray, axis=0)

#Displaying the array consisting of cumulative sum of all the elements along the axis=0 in the array

print 'The array consisting of cumulative sum of all the elements along the axis=0 of the given array using cumsum function is as follows:'

print cumsumarrayalongaxis0

cumsumarrayalongaxis1 = nump.cumsum(initialarray, axis=1)

#Displaying the array consisting of cumulative sum of all the elements along the axis=1 in the array

print 'The array consisting of cumulative sum of all the elements along the axis=1 of the given array using cumsum function is as follows:'

print cumsumarrayalongaxis1

4.5 (6,869 ratings)

View Course

**Output:**

In the above program, a package called numpy is imported which allows us to make use of functions called array and cumsum. Then an array called initialarray is created by making use of array function. Then the elements of initialarray are displayed on the screen. Then cumsum function along with the axis specified is used to find the cumulative sum of all the elements in initialarray and stored in a variable called cumsumarray. Then the cumsumarray consisting of cumulative sum of all the elements along the axis=0 and along the axis=1 is displayed on the screen.

### Recommended Articles

This is a guide to NumPy cumsum. Here we discuss the introduction, working of NumPy cumsum function along with examples respectively. You may also have a look at the following articles to learn more –