AppDividend
Latest Code Tutorials

np.cumsum: How to Use numpy cumsum() Function in Python

The np.cumsum() is a numpy library function that returns the cumulative sum of the elements along the given axis. The numpy cumsum() function returns an array. The array consists of the cumulative sum of the original array. The cumulative sum is not similar to the sum function.

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 given as 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.

Python program for calculating the sum from the cumulative sum using np.cumsum() function

# 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. To do that, we passed the array into the cumsum() function. The cumsum() function finds the cumulative sum then we have returned only the last value from the array by the -1 indexing. It returns the last element from the array.

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

Python program for calculating the cumulative sum using np.cumsum() function

# 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 sum of the current element plus the cumulative sum of the previous element. The result is displayed.

The arr array consists of four rows, each row consisting of 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 for the numpy cumsum() function.

Related posts

np.power()

np.float_power()

np.divide()

np.true_divide()

np.floor_divide()

Leave A Reply

Your email address will not be published.

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