Python:NumPy .cumsum()
Published Oct 30, 2025
Contribute to Docs
The .cumsum() method in NumPy computes the cumulative sum of elements along a specified axis. If no axis is provided, it returns the cumulative sum of the flattened array.
Syntax
ndarray.cumsum(axis=None, dtype=None, out=None)
Parameters:
axis(Optional): Specifies the axis along which the cumulative sum is computed. If the axis is not specified, the cumsum is computed over the flattened array.dtype(Optional): Specifies the data type of the returned array. If not provided, it defaults to the data type of the input array. However, if the array has an integer type with a precision lower than that of the default platform integer, the default platform integer is used instead.out(Optional): An optional output array in which to store the resulting cumulative sum. The shape and buffer length must match that of the expected output but the type will be cast if necessary.
Return value:
Returns an array containing the cumulative sum of elements, or a reference to out if specified.
Example 1: Computing Cumulative Sum with and without Axis
This example calculates the cumulative sum of a 2D array:
import numpy as nparray = np.array([[1, 2, 3], [4, 5, 6]])# Cumulative sum of flattened arraycumsum_flat = array.cumsum()# Cumulative sum along rows (axis=1)cumsum_axis1 = array.cumsum(axis=1)# Cumulative sum along columns (axis=0)cumsum_axis0 = array.cumsum(axis=0)print("Flattened cumulative sum:\n", cumsum_flat)print("\nCumulative sum along rows:\n", cumsum_axis1)print("\nCumulative sum along columns:\n", cumsum_axis0)
The output of this code is:
Flattened cumulative sum:[ 1 3 6 10 15 21]Cumulative sum along rows:[[ 1 3 6][ 4 9 15]]Cumulative sum along columns:[[1 2 3][5 7 9]]
Here:
- When no axis is specified, NumPy flattens the array before computing the cumulative sum.
- When an axis is specified, it computes the sum along the given dimension.
Example 2: Using dtype and Output Array
In this example, a custom data type (float) is specified for the cumulative sum, and the result is stored in a preallocated output array:
import numpy as nparray = np.array([1, 2, 3])# Create an empty array for outputoutput_array = np.empty_like(array, dtype=float)# Compute cumulative sum with custom dtype and outputarray.cumsum(dtype=float, out=output_array)print("Cumulative sum with float dtype:")print(output_array)
The output of this code is:
Cumulative sum with float dtype:[1. 3. 6.]
Codebyte Example
In this example, the cumulative sum of a 2D array is computed along both axes using different data types for each:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours