NumPy ndarray.cumsum()

The numpy.ndarray.cumsum() method returns the cumulative sum of the elements in a NumPy array. It can compute the sum over the entire array or along a specified axis.

Syntax

</>
Copy
ndarray.cumsum(axis=None, dtype=None, out=None)

Parameters

ParameterTypeDescription
axisNone, int, optionalSpecifies the axis along which the cumulative sum is computed. If None, the sum is computed over the entire array.
dtypedata-type, optionalDefines the type of the output array. If not specified, it uses the input array’s dtype.
outndarray, optionalAlternative output array for storing the result. Must have the same shape as the expected output.

Return Value

Returns a NumPy array of the same shape as the input, where each element is the cumulative sum of the preceding elements.


Examples

1. Computing the Cumulative Sum of a 1D Array

In this example, we compute the cumulative sum of a one-dimensional array.

</>
Copy
import numpy as np

# Creating a 1D NumPy array
arr = np.array([1, 2, 3, 4, 5])

# Computing the cumulative sum of the entire array
result = arr.cumsum()

# Printing the result
print(result)  # Output: [ 1  3  6 10 15]

Output:

[ 1  3  6 10 15]

Each element in the output represents the sum of all previous elements in the array up to that index.

2. Using the axis Parameter in ndarray.cumsum()

Here, we compute the cumulative sum along different axes in a 2D array.

</>
Copy
import numpy as np

# Creating a 2D NumPy array
arr = np.array([[1, 2, 3], 
                [4, 5, 6]])

# Cumulative sum along axis=0 (column-wise)
result_axis0 = arr.cumsum(axis=0)
print("Cumulative sum along axis 0 (column-wise):\n", result_axis0)

# Cumulative sum along axis=1 (row-wise)
result_axis1 = arr.cumsum(axis=1)
print("Cumulative sum along axis 1 (row-wise):\n", result_axis1)

Output:

Cumulative sum along axis 0 (column-wise):
[[ 1  2  3]
 [ 5  7  9]]

Cumulative sum along axis 1 (row-wise):
[[ 1  3  6]
 [ 4  9 15]]

For axis=0, the sum accumulates down each column, whereas for axis=1, the sum accumulates across each row.

3. Specifying Output Data Type Using dtype

We can specify the data type of the result using the dtype parameter.

</>
Copy
import numpy as np

# Creating an array with integer values
arr = np.array([1, 2, 3, 4], dtype=np.int32)

# Computing the cumulative sum and storing the result as float
result = arr.cumsum(dtype=np.float64)

print(result)  # Output: [ 1.  3.  6. 10.]

Output:

[ 1.  3.  6. 10.]

By specifying dtype=np.float64, the result is stored in floating-point format instead of integers.

4. Storing Output in a Specified Array Using out

The out parameter allows storing the result in an existing array.

</>
Copy
import numpy as np

# Creating an input array
arr = np.array([1, 2, 3, 4])

# Creating an output array of the same shape
out_arr = np.empty_like(arr)

# Computing cumulative sum and storing in out_arr
arr.cumsum(out=out_arr)

print(out_arr)  # Output: [ 1  3  6 10]

Output:

[ 1  3  6 10]

The cumulative sum is stored in out_arr instead of creating a new array.