What is the numpy.cumsum() Method

The numpy.cumsum() function “returns the cumulative sum of the elements along the given axis”. The cumulative sum is the sum of all previous elements in the array. For example, the cumulative sum of the array [1, 2, 3] is [1, 3, 6].

Syntax

numpy.cumsum(arr, axis=None, dtype=None, out=None) 

Parameters

The np.cumsum() function has one required argument and three optional arguments as parameters:

  1. arr: The array is passed in this argument. This value is the argument needed for returning the cumulative sum of the array. This array is given inside the cumsum() function for finding the cumulative sum of this array. 
  2. axis: The axis along which the cumulative sum is calculated is the value of this axis argument. This axis is kept as None by default. However, the axis can be changed.
  3. dtype: The dtype stands for Data type. This specifies the output data type. The output data will be returned in this specified data type.
  4. out: This is the destination where the output array is stored. This array size specified must match the output obtained from the function.

Return value

The np.cumsum() function returns an array as output. The cumsum() function finds the cumulative sum for the array and returns the cumulative sum in the array format.

Example 1

# Importing numpy as np
import numpy as np

# Creating an numpy array called arr
arr = np.array([5, 6, 7, 8, 9, 10, 11, 12, 19, 25])

# Printing the array arr
print("The array arr is: ")
print(arr)

# Printing the shape of the array
print("The shape of the array arr is : ", arr.shape)

# Finding the sum from the cumulative sum function for the array
res = np.cumsum(arr)[-1]

# Printing the sum of the array
print("The sum of the array using the cumulative sum function is:")
print(res)

Output

The array arr is:
[ 5 6 7 8 9 10 11 12 19 25]
The shape of the array arr is : (10,)
The sum of the array using the cumulative sum function is:
112

We imported the numpy package that provides several array functions in this program. Then, we created a numpy array called arr using the function called np.array(). This arr array consists of several integer elements.

For this array, the sum is calculated using the cumulative sum function.

We passed the array into the cumsum() function to do that. The cumsum() function finds the cumulative sum then we return only the last value from the array by the -1 indexing. So it returns the last element from the array.

The last element in the array is the sum of all the elements. Hence we have printed the sum of all the elements using the cumsum() function.

Example 2

# Importing numpy as np
import numpy as np

# Creating an numpy array called a
a = np.array([5, 6, 7, 8, 9, 10, 11, 12, 19, 25])

# printing the array a
print("The array a is: ")
print(a)

# Printing the shape of the array a
print("The shape of the array arr is : ", a.shape)

# Finding the cumulative sum of the single dimensional array
op = np.cumsum(a)

# Printing the sum of the array
print("The cumulative sum of the array is: ")
print(op)

# Creating an numpy array called arr
arr = np.array([[1, 4, 0], [7, 0, 9], [5, 0, 3], [12, 0, 5]])

# printing the array arr
print("The array arr is: ")
print(arr)

# Printing the shape of the array
print("The shape of the array arr is : ", arr.shape)

# Finding the cumulative sum for the two dimensional array
res = np.cumsum(arr, axis=1)

# Printing the cumulative sum of the array
print("The cumulative sum of the array is: ")
print(res)

Output

The array a is:
[ 5 6 7 8 9 10 11 12 19 25]
The shape of the array arr is : (10,)
The cumulative sum of the array is:
[ 5 11 18 26 35 45 56 68 87 112]
The array arr is:
[[ 1 4 0]
[ 7 0 9]
[ 5 0 3]
[12 0 5]]
The shape of the array arr is : (4, 3)
The cumulative sum of the array is:
[[ 1 5 5]
[ 7 7 16]
[ 5 5 8]
[12 12 17]]

We created two numpy arrays called a and arr using the function called np.array().

The “a” array is a single-dimensional array. This array consists of some integer values. We passed this array into the np.cumsum() function.

The np.cumsum() function calculates the cumulative sum of the array. The cumulative sum is the current element’s sum plus the previous element’s cumulative sum. The result is displayed.

The arr array consists of four rows, each with 3 columns. Then we passed this array to the np.cumsum() function with the axis as 1. The cumsum() function calculates the cumulative sum and returns the array.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.