NumPy cumsum()

The cumsum() function is used to calculate the cumulative sum of array elements along a specified axis or across all axes.

Example

import numpy as np

# create a NumPy array
array1 = np.array([1, 2, 3, 4, 5])

# calculate the cumulative sum of the array elements cumulative_sum = np.cumsum(array1)
print(cumulative_sum) # Output : [ 1 3 6 10 15]

cumsum() Syntax

The syntax of cumsum() is:

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

cumsum() Arguments

The cumsum() function takes following arguments:

  • array - the input array
  • axis (optional) - the axis along which the cumulative sum is calculated
  • dtype (optional) - the data type of the returned cumulative sum
  • out (optional) - the output array where the result will be stored

cumsum() Return Value

The cumsum() function returns an array containing the cumulative sum of elements along the specified axis or across all axes.


Example 1: cumsum() With 2-D Array

The axis argument defines how we can find the sum of elements in a 2-D array.

  • If axis = None, the array is flattened and the cumulative sum of the flattened array is returned.
  • If axis = 0, the cumulative sum is calculated column-wise.
  • If axis = 1, the cumulative sum is calculated row-wise.

Let's see an example.

import numpy as np

# create a 2-D array
array1 = np.array([[1, 2, 3],
                                [4, 5, 6],
                                [7, 8, 9]])

# calculate cumulative sum of elements of the flattened array result1 = np.cumsum(array1)
print('Cumulative Sum of flattened array: ', result1)
# calculate the cumulative sum along the rows (axis=1) cumulative_sum_rows = np.cumsum(array1, axis=1)
# calculate the cumulative sum along the columns (axis=0) cumulative_sum_cols = np.cumsum(array1, axis=0)
print("\nCumulative sum along rows:") print(cumulative_sum_rows) print("\nCumulative sum along columns:") print(cumulative_sum_cols)

Output

Cumulative Sum of flattened array:  [ 1  3  6 10 15 21 28 36 45]

Cumulative sum along rows:
[[ 1  3  6]
 [ 4  9 15]
 [ 7 15 24]]

Cumulative sum along columns:
[[ 1  2  3]
 [ 5  7  9]
 [12 15 18]]

Example 2: Use of dtype Argument in cumsum()

import numpy as np

# create an array
array1 = np.array([1, 2, 3, 4, 5])

# calculate the cumulative sum of array1 with the specified data type as float cumulative_sum_float = np.cumsum(array1, dtype=float)
print(cumulative_sum_float)

Output

[ 1.  3.  6. 10. 15.]

Here, the dtype argument is used to specify the data type of the resulting cumulative sum.

In our case, we set the data type as float using dtype=float. This means that the resulting cumulative sum will have elements of type float.


Example 3: Use of out Argument in cumsum()

import numpy as np

# create an array
array1 = np.array([1, 2, 3, 4, 5])

# create an empty array with the same shape as array1
out_array = np.zeros_like(array1)

# calculate the cumulative sum of array1 and store the result in out_array np.cumsum(array1, out=out_array)
print(out_array)

Output

[ 1  3  6 10 15]
[ 1.  3.  6. 10. 15.]

Here, after specifying out=out_array, the result of cumulative sum of array1 is stored in the out_array array.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges