numpy.cumsum() in Python — Compute Cumulative Sums with Clear Examples
Learn how numpy.cumsum() computes cumulative sums in Python. Step-by-step examples show use without axis, along axes, and with dtype control for accurate array
Drake Nguyen
Founder · System Architect
Overview: numpy.cumsum() in Python
The numpy.cumsum() function returns the cumulative sum of array elements. It’s a common tool for aggregating values across an array or along a specific axis, and it works with NumPy ndarrays and array-like inputs such as nested lists. This guide shows syntax, practical examples, and tips for using numpy.cumsum effectively in Python.
Syntax
Function signature:
cumsum(array, axis=None, dtype=None, out=None)
Parameters:
- array — Input ndarray or array-like (for example, nested lists).
- axis — The axis along which the cumulative sum is computed. If
None(default), the input is flattened first and the cumulative sum is computed on the flattened array. - dtype — Optional output data type (for example
floatorint); useful to prevent overflow or to convert integers to floats. - out — Optional output array to store the result.
Examples: Compute Cumulative Sums
The examples below demonstrate common usage patterns for numpy.cumsum(), including use without an axis, along axes, and with a specified data type.
1. Cumulative sum without an axis (flattened)
import numpy as np
array1 = np.array(
[[1, 2],
[3, 4],
[5, 6]])
total = np.cumsum(array1)
print(f'Cumulative Sum of all the elements is {total}')
Output: [ 1 3 6 10 15 21]. The array is flattened to [1, 2, 3, 4, 5, 6] and the cumulative sum is calculated sequentially.
2. Cumulative sum along a specific axis
import numpy as np
array1 = np.array(
[[1, 2],
[3, 4],
[5, 6]])
# Cumulative sum down each column (axis=0)
total_0_axis = np.cumsum(array1, axis=0)
print(f'Cumulative Sum of elements at 0-axis is:\n{total_0_axis}')
# Cumulative sum across each row (axis=1)
total_1_axis = np.cumsum(array1, axis=1)
print(f'Cumulative Sum of elements at 1-axis is:\n{total_1_axis}')
Example output:
[[ 1 2]
[ 4 6]
[ 9 12]]
[[ 1 3]
[ 3 7]
[ 5 11]]
Use axis=0 to accumulate down columns and axis=1 to accumulate across rows.
3. Specify the output data type (dtype)
import numpy as np
array1 = np.array(
[[1, 2],
[3, 4],
[5, 6]])
total_1_axis = np.cumsum(array1, axis=1, dtype=float)
print(f'Cumulative Sum of elements at 1-axis is:\n{total_1_axis}')
Output:
[[ 1. 3.]
[ 3. 7.]
[ 5. 11.]]
Specifying dtype=float forces floating-point accumulation, which helps avoid integer overflow and preserves fractional results when needed.
Tips and Common Pitfalls
- Remember that
axis=Noneflattens the input first; set an explicitaxisto preserve the array shape during accumulation. - Choose
dtypecarefully to avoid overflow for large integer arrays. - Use the
outparameter to write results into a preallocated array when memory or performance is a concern.
Further reading
For full details and edge cases, consult the official NumPy cumsum API documentation.