Python NumPy cumsum() Function

Spread the love

Python NumPy cumsum() function is used to return the cumulative sum of the array elements along the given axis. If the axis is provided, it will return the array with the cumulative sum of elements along with the provided axes. In this article, I will explain syntax and how to use the numpy.cumsum() function which returns a cumulative sums of the input array.

1. Quick Examples of NumPy cumsum() Function

If you are in a hurry, below are some quick examples of how to use cumsum() function in NumPy.


# Below are the quick examples

import numpy as np

# Example 1: Get the cumsum of integer
arr = 12
arr1 = np.cumsum(arr)

# Example 2: Get the cumulative sum of 1-d array
arr = np.array([2, 7, 5, 8, 9,4])
arr1 = np.cumsum(arr)

# Example 3: Get the cumulative sum of numpy array
arr = np.array([[5, 8, 3, 7], [9, 4, 2, 6],[12, 8, 14, 11]])
arr1 = np.cumsum(arr)

# Example 4: Get cumulative sum over rows 
# For each of 4 columns
arr1 = np.cumsum(arr, axis = 0)

# Example 5: Get cumulative sum over columns 
# For each of 3 rows
arr1 = np.cumsum(arr, axis = 1)

# Example 6: Get the cumulative sum of an array
# Along with specified datatype
arr1 = np.cumsum(arr, dtype = float)

# Example 7: Get cumulative sum over columns 
# For each of 3 rows along with datatype
arr1 = np.cumsum(arr, axis=1, dtype=float) 

2. Syntax of cumsum()

Following is the syntax of the cumsum() function.


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

2.1 Parameters of cumsum()

  • arr – The input array containing numbers whose cumulative sum is desired. If arr is not an array, a conversion is attempted.
  • axis – The axis along which the cumulative sum is computed. The default is to compute the sum of the flattened array.
  • dtype – Type of the returned array, as well as of the accumulator in which the elements are multiplied. If dtype is not specified, it defaults to the dtype of arr, unless arr has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used instead.
  • out – It is a ndarray and an optional . A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

2.2 Return Value of cumsum()

It returns a new array holding the result unless out is specified.

3. Usage of NumPy cumsum() Function

Use numpy.cumsum() function to compute the cumulative sum of array elements over a given axis. The process of formation of the cumulative sum of an array is, it keeps the first element as is, the second element is the sum of the first element and the second element, the third element is the sum of the first, second, and the third element, and so on.

When you used a single scalar value to cumsum(), it returns the same value. For example,


import numpy as np

# Get the cumsum of integer
arr = 12
arr1 = np.cumsum(arr)
print(arr1)

# Output:
# [12]

4. Get the Cumulative sum of a 1-D NumPy Array

Let’s use a NumPy 1-D array and calculate the cumulative sum, to do so first create an NumPy array using numpy.array() function and pass the array as an argument to the function. This returns the array with cumulative sum values.


# Creating an 1D input array
arr = np.array([2, 7, 5, 8, 9,4])

# Get the cumulative sum of 1-d array 
arr1 = np.cumsum(arr)
print(arr1)

# Output:
# [ 2  9 14 22 31 35]

5. Get the Cumulative sum of a 2-D NumPy Array

When we use cumsum() to compute the cumulative sum of the 2-D NumPy array, it will return the cumulated sum of the flattened array.


# Get the cumulative sum of numpy array
arr = np.array([[5, 8, 3, 7], [9, 4, 2, 6],[12, 8, 14, 11]])
arr1 = np.cumsum(arr)
print(arr1)

# Output:
# [ 5 13 16 23 32 36 38 44 56 64 78 89]

6. Get the Cumulative sum Over Rows of a 2-D Array

Let’s compute the cumulative sum of an array along with specified axis=0 , This calculates the cumulative sum vertically or column-wise.


# Get cumulative sum over rows 
# For each of 4 columns
arr1 = np.cumsum(arr, axis = 0)
print(arr1)
 
# Output:
# [[ 5  8  3  7]
# [14 12  5 13]
# [26 20 19 24]]

7. Get the Cumulative sum Over Columns of a 2-D Array

By using axis =1, it calculates the cumulative sum horizontally or row-wise.


# Get cumulative sum over columns 
# For each of 3 rows
arr1 = np.cumsum(arr, axis = 1)
print(arr1)

# Output:
# [[ 5 13 16 23]
# [ 9 13 15 21]
# [12 20 34 45]]

8. Get the Cumulative sum of a 2-D Array along Data Type

If you want a cumulative sum output array in a specific data type use the dtype argument. The following example returns the array in float type.


# Get the cumulative sum of an array
# Along with specified datatype
arr1 = np.cumsum(arr, dtype = float)
print(arr1)

# Output:
# [ 5. 13. 16. 23. 32. 36. 38. 44. 56. 64. 78. 89.]

Alternately, the above code can be customized output along with the specified axis.


# Get cumulative sum over columns 
# GHfor each of 3 rows along with datatype
arr1 = np.cumsum(arr, axis=1, dtype=float)
print(arr1)

# Output:
# [[ 5. 13. 16. 23.]
# [ 9. 13. 15. 21.]
# [12. 20. 34. 45.]]

9. Conclusion

In this article, I have explained how to use NumPy.cumsum() function and using this how to compute the cumulative sum of NumPy array along with specified axis and specified datatype.

Happy Learning!!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Python NumPy cumsum() Function