How to use ndarray.cumprod() method in NumPy (4 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

Introduction

In the world of data analysis and scientific computing, NumPy stands as a pillar for numerical operations in Python. Among its plethora of functions, ndarray.cumprod() is a powerful method used for computing the cumulative product of array elements over a specified axis. This tutorial aims to provide comprehensive guidance on utilizing the ndarray.cumprod() method through four progressively complex examples.

Understanding ndarray.cumprod()

Syntax:

arr.cumprod(axis=None, dtype=None, out=None)

Where arr is an instance of ndarray.

Parameters explained:

  • axis: This optional parameter specifies the axis along which the cumulative product is computed. By default, it’s set to None, meaning the cumulative product is computed over the flattened array, treating it as a single sequence of values. If you specify an axis (e.g., 0 for columns or 1 for rows in a 2D array), the cumulative product is calculated along that axis, separately for each slice along the axis.
  • dtype: Sometimes, the cumulative product of an array can result in values that are too large for the default data type of the input array to handle. The dtype parameter allows you to specify a different data type for the calculation and the resulting array. For example, using dtype=np.int64 for an array of integers that might otherwise overflow.
  • out: If you want to store the result of cumprod in a pre-existing array rather than creating a new one, you can use this parameter to specify that output array. The shape of the out array must match the expected shape of the operation’s resul

The ndarray.cumprod() method returns the cumulative product of the elements along a given axis. If no axis is specified, the cumulative product is calculated over the flattened array. This technique is instrumental in scenarios where you need to compute the product of elements cumulatively, such as in calculating factorial numbers, product-based probability calculations, and more.

Example 1: Basic Usage

Let’s start with the most basic example – calculating the cumulative product of a one-dimensional array.

import numpy as np

# Create a one-dimensional NumPy array
arr = np.array([1, 2, 3, 4])

# Calculate the cumulative product
arr_cumprod = arr.cumprod()

print(arr_cumprod)

Output:

[1 2 6 24]

This output demonstrates how the cumulative product builds up; the product of the first and second elements is 2, the product of the second and the third is 6, and so on.

Example 2: Specifying the Axis in a Multidimensional Array

Moving to a more advanced scenario, consider a two-dimensional array. Specifying an axis becomes crucial here.

import numpy as np

# Create a two-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Calculate the cumulative product along the first axis (down the rows)
arr_cumprod_row = arr.cumprod(axis=0)

# Calculate the cumulative product along the second axis (across the columns)
arr_cumprod_col = arr.cumprod(axis=1)

print('Cumulative product down the rows:\n', arr_cumprod_row, '\n')
print('Cumulative product across the columns:\n', arr_cumprod_col)

Output:

Cumulative product down the rows:
 [[ 1  2  3]
  [ 4 10 18]] 

Cumulative product across the columns:
 [[  1   2   6]
  [  4  20 120]]

This illustrates the importance of the axis parameter. When calculating down the rows (axis=0), each position holds the cumulative product of values in the same column from all preceding rows. Conversely, calculating across the columns (axis=1), each position contains the cumulative product of values in the same row from all preceding columns.

Example 3: Working with Higher-Dimensional Arrays

Next, let’s explore higher-dimensional arrays to showcase the versatility of ndarray.cumprod().

import numpy as np

# Create a three-dimensional array
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Calculate the cumulative product along the last axis (-1)
arr_cumprod_last_axis = arr.cumprod(axis=-1)

print('Cumulative product along the last axis:\n', arr_cumprod_last_axis)

Output:

Cumulative product along the last axis:
 [[[  1   2]
   [  3  12]]
  [[  5  30]
   [  7  56]]]

Here, each sub-array’s last axis undergoes the cumulative product calculation, demonstrating ndarray.cumprod()‘s ability to navigate through complex dimensions.

Example 4: Handling Special Cases – Empty Arrays and NaN Values

It’s invaluable to understand how ndarray.cumprod() handles edge cases, such as empty arrays and arrays with NaN (Not a Number) values.

import numpy as np

# Empty array
empty_arr = np.array([])
empty_arr_cumprod = empty_arr.cumprod()
print('Cumulative product of an empty array:', empty_arr_cumprod, '\n')

# Array with NaN values
nan_arr = np.array([1, np.nan, 3])
nan_arr_cumprod = nan_arr.cumprod()
print('Cumulative product with NaN values:', nan_arr_cumprod)

Output:

Cumulative product of an empty array: [] 

Cumulative product with NaN values: [ 1. nan nan]

This shows that cumulative product operations on empty arrays yield empty arrays. Meanwhile, encountering a NaN value results in subsequent cumulative products being NaN, as NaN propagates through arithmetic operations.

Conclusion

Throughout this tutorial, we ventured from the basics of using ndarray.cumprod() in NumPy to more sophisticated applications, covering multidimensional arrays and special cases. Understanding and employing this method opens up a realm of possibilities for data analysis, allowing for the easy computation of cumulative products in a wide array of scenarios.