How to Use Numpy cumsum() Function in Python

In the Numpy module, we have discussed many functions used to operate on the multidimensional array. Sometimes, we occur when we want to calculate the cumulative sum of the element present in the given array. In this tutorial, we will discuss the concept of the numpy cumsum() function, which is used when we want to compute the cumulative sum of array elements over a given axis.

What is the Numpy cumsum() function?

The Numpy cumsum() function is used when we need to compute the cumulative sum of array elements. If the axis is provided, it will return the array with the cumulative sum of elements along the axes. The cumulative sum of an array is like the first element, the second element is the sum of the first element and the second element, the third element is the sum of the first element, the second element, and the third element, and so on.

Syntax of Numpy cumsum

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

Parameters

  • arr: [Array-like]. It is an input array containing numbers whose cumulative sum is desired. If arr is not an array, a conversion is done.
  • axis: It is an integer value that is an optional input. It is the axis along which cumulative sum is computed. By default, it is None that computed the sum of the flattened array.
  • Dtype: It is an optional input. It is the type of returned array and the accumulator in which the elements are summed. If we don’t specify dtype, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used.
  • out: It is a ndarray and an optional input. It is the location where we store the result.
    • If we provide it, it must have a shape to which the inputs broadcast.
    • A freshly allocated array is returned if we don’t provide it.

Return Value of Numpy cumsum

The return value is a new array unless out is specified, in which case it is returned.

Example of Numpy cumsum() function?

Here, we will be discussing how we can write the cumsum() function from the numpy library.

1. Taking only a variable as input

In this example, we will import the numpy library in Python. We will not be taking input as an array. We will take an integer variable and then apply the numpy cumsum() function. Hence, we will see the conversion is done. Let us look at the example for understanding the concept in detail.

#taking input as a variable
#importing numpy library
import numpy as np

a = 15
print("input taken : ",a)
output = np.cumsum(a)
print("conversion done : ",output)

Output:

input taken :  15
conversion done :  [15]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will be taking a variable a which has the integer value 15 stored in it.
  • After that, we will print the input value.
  • Then, we will apply the numpy cumsum() function and store the output in the output variable.
  • At last, we will print the output.
  • Hence, you can see the output as the conversion done as it was the single integer value.

2. Taking input as array

In this example, we will import the numpy module. We will then be taking input as an array and then applying the numpy cumsum() function. At last, we will see the cumulative sum of the array. Let us look at the example for understanding the concept in detail.

#taking input as an array
#importing numpy library
import numpy as np

arr = np.array([[1,2,3,4], [5,3,6,7]])
print("input taken : ",arr)
output = np.cumsum(arr)
print("Cumulative sum of array : ",output)

Output:

input taken :  [[1 2 3 4]
 [5 3 6 7]]
Cumulative sum of array :  [ 1  3  6 10 15 18 24 31]

Explanation:

  • Firstly, we will import the numpy library with an alias named np.
  • Then, we will take input as an array with some integer values inside it.
  • After that, we will print the input array.
  • Then, we will apply the numpy cumsum() function and store the output in the output variable.
  • At last, we will print the output.
  • Hence, you can see the output as the cumulative sum of the array.

3. Taking an array and axis as a parameter

In this example, we will import the numpy module. Then, we will take input as an array and apply the axis as 0 and 1 as a parameter. At last, we will see the output on both axis and see the difference. Let us look at the example for understanding the concept in detail.

#taking input as an array and axis parameter
#importing numpy library
import numpy as np

arr = np.array([[1,2,3,4], [5,3,6,7]])
print("input taken : ",arr)
output = np.cumsum(arr, axis = 0)
print("Cumulative sum of array at axis = 0: ",output)
out = np.cumsum(arr, axis = 1)
print("Cumulative sum of array at axis = 1 : ",out)

Output:

input taken :  [[1 2 3 4]
 [5 3 6 7]]
Cumulative sum of array at axis = 0:  [[ 1  2  3  4]
 [ 6  5  9 11]]
Cumulative sum of array at axis = 1 :  [[ 1  3  6 10]
 [ 5  8 14 21]]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take input as an array with some integer values inside it.
  • After that, we will print the input array.
  • We will then apply the numpy cumsum() function with an array, and the axis equals 0 and 1 and stores the output in the output and out variable.
  • Finally, we will print both outputs.
  • Hence, you can see the output as the cumulative sum of the array with axis = 0 and axis = 1.

4. Taking an array and dtype as a parameter

In this example, we will import the numpy module. Then, we will take input as an array and dtype = float as the datatype of the array. At last, we will see the output. Let us look at the example for understanding the concept in detail.

#taking input as an array and dtype as a parameter
#importing numpy library
import numpy as np

arr = np.array([[1,2,3,4], [5,3,6,7]])
print("input taken : ",arr)
output = np.cumsum(arr, dtype = float)
print("Cumulative sum of array : ",output)

Output:

input taken :  [[1 2 3 4]
 [5 3 6 7]]
Cumulative sum of array :  [ 1.  3.  6. 10. 15. 18. 24. 31.]

Explanation:

  • Firstly, we will import the numpy library with an alias named np.
  • Then, we will take input as an array with some integer values inside it.
  • After that, we will print the input array.
  • Then, we will apply the numpy cumsum() function with array and type = float as a parameter and store the output in the output variable.
  • At last, we will print the output.
  • Hence, you can see the output as the cumulative sum of the array in the float data type.

Conclusion

In this tutorial, we have learned about the concept of the numpy cumsum() function. We got to know what is numpy cumsum() function and how the cumulative sum of the given array element is calculated. We have explained all the multiple ways through which we can calculate the cumulative sum. All the ways are explained with examples in detail. You can use any of the functions according to your choice and your requirement in the program.

However, please let me know in the comment section below if you have any questions. I will try to help you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments