NumPy: Calculate cumulative sum and product (np.cumsum, np.cumprod)
In NumPy, you can calculate the cumulative sum and product using the numpy.cumsum()
, numpy.cumprod()
functions or the cumsum()
and cumprod()
methods of numpy.ndarray
.
- numpy.cumsum — NumPy v1.24 Manual
- numpy.cumprod — NumPy v1.24 Manual
- numpy.ndarray.cumsum — NumPy v1.24 Manual
- numpy.ndarray.cumprod — NumPy v1.24 Manual
This article covers the following topics:
- The cumulative sum:
numpy.cumsum()
- The cumulative product:
numpy.cumprod()
You can also calculate the cumulative sum and product using functions and methods from Python's standard library, itertools, and pandas. With itertools, you can apply any function cumulatively.
- Calculate cumulative sum and product in Python (itertools.accumulate)
- pandas: Cumulative calculations (cumsum, cumprod, cummax, cummin)
The cumulative sum: numpy.cumsum()
Basic usage
Consider the following one-dimensional numpy.ndarray
:
import numpy as np
print(np.__version__)
# 1.19.0
a_1d = np.array([1, 2, 3, 4, 5, 6])
print(a_1d)
# [1 2 3 4 5 6]
You can obtain the cumulative sum with np.cumsum()
. The function returns a numpy.ndarray
, and you can specify its data type using the dtype
argument.
print(np.cumsum(a_1d))
# [ 1 3 6 10 15 21]
print(np.cumsum(a_1d, dtype=float))
# [ 1. 3. 6. 10. 15. 21.]
The cumsum()
method is also available for numpy.ndarray
, with the same arguments such as dtype
and axis
, as described later, as those for the np.cumsum()
function.
print(a_1d.cumsum())
# [ 1 3 6 10 15 21]
print(a_1d.cumsum(dtype=float))
# [ 1. 3. 6. 10. 15. 21.]
The first argument of np.cumsum()
can be an array-like object, such as a list, not just a numpy.ndarray
. However, the result is always a numpy.ndarray
.
l = [1, 2, 3, 4, 5, 6]
print(np.cumsum(l))
# [ 1 3 6 10 15 21]
print(type(np.cumsum(l)))
# <class 'numpy.ndarray'>
To convert a numpy.ndarray
to a list, refer to the following article:
For multi-dimensional arrays
Consider the following two-dimensional numpy.ndarray
:
a_2d = a_1d.reshape(2, 3)
print(a_2d)
# [[1 2 3]
# [4 5 6]]
By default, a flattened one-dimensional cumulative sum is generated for multi-dimensional arrays.
print(np.cumsum(a_2d))
# [ 1 3 6 10 15 21]
By specifying the axis
argument, you can calculate the cumulative sum for each axis (dimension). For a two-dimensional array, axis=0
is column-wise, and axis=1
is row-wise.
print(np.cumsum(a_2d, axis=0))
# [[1 2 3]
# [5 7 9]]
print(np.cumsum(a_2d, axis=1))
# [[ 1 3 6]
# [ 4 9 15]]
The same applies to the cumsum()
method of numpy.ndarray
.
print(a_2d.cumsum())
# [ 1 3 6 10 15 21]
print(a_2d.cumsum(axis=0))
# [[1 2 3]
# [5 7 9]]
print(a_2d.cumsum(axis=1))
# [[ 1 3 6]
# [ 4 9 15]]
The same applies when specifying a list of lists (= 2D list) as the first argument of np.cumsum()
.
l_2d = [[1, 2, 3], [4, 5, 6]]
print(np.cumsum(l_2d))
# [ 1 3 6 10 15 21]
print(np.cumsum(l_2d, axis=0))
# [[1 2 3]
# [5 7 9]]
print(np.cumsum(l_2d, axis=1))
# [[ 1 3 6]
# [ 4 9 15]]
Note that if the inner lists contain different numbers of elements, the result might not be as expected. In this case, it is treated as an array with each list as an element instead of a multi-dimensional array.
l_2d_error = [[1, 2, 3], [4, 5]]
print(np.cumsum(l_2d_error))
# [list([1, 2, 3]) list([1, 2, 3, 4, 5])]
The cumulative product: numpy.cumprod()
You can calculate the cumulative product using numpy.cumprod()
. The usage is the same as for numpy.cumsum()
mentioned above. The cumprod()
method is also provided for numpy.ndarray
.
print(np.cumprod(a_1d))
# [ 1 2 6 24 120 720]
print(np.cumprod(a_1d, dtype=float))
# [ 1. 2. 6. 24. 120. 720.]
print(a_1d.cumprod())
# [ 1 2 6 24 120 720]
print(a_1d.cumprod(dtype=float))
# [ 1. 2. 6. 24. 120. 720.]
For multi-dimensional arrays:
print(np.cumprod(a_2d))
# [ 1 2 6 24 120 720]
print(np.cumprod(a_2d, axis=0))
# [[ 1 2 3]
# [ 4 10 18]]
print(np.cumprod(a_2d, axis=1))
# [[ 1 2 6]
# [ 4 20 120]]
print(a_2d.cumprod())
# [ 1 2 6 24 120 720]
print(a_2d.cumprod(axis=0))
# [[ 1 2 3]
# [ 4 10 18]]
print(a_2d.cumprod(axis=1))
# [[ 1 2 6]
# [ 4 20 120]]
Like numpy.cumsum()
, you can specify array-like objects, such as lists, as arguments for numpy.cumprod()
.
print(np.cumprod(l))
# [ 1 2 6 24 120 720]
print(np.cumprod(l_2d))
# [ 1 2 6 24 120 720]
print(np.cumprod(l_2d, axis=0))
# [[ 1 2 3]
# [ 4 10 18]]
print(np.cumprod(l_2d, axis=1))
# [[ 1 2 6]
# [ 4 20 120]]