.prod()

itispragativerma6560850080's avatar
Published Dec 1, 2024
Contribute to Docs

The numpy.prod() function computes the product of all elements in an array, or along a specified axis.

This method is particularly useful in scenarios such as calculating factorial-like operations, determining probabilities in statistics, or finding cumulative products in multidimensional datasets.

By specifying the axis, the function can target specific rows or columns in multidimensional arrays, making it versatile for matrix and tensor operations.

Syntax

numpy.prod(a, axis=None, dtype=None, out=None, keepdims=<no value>)
  • a: Input array for which the product is calculated.
  • axis(Optional): The axis or axes along which the product is computed. If not specified, the product of all elements in the array is returned.
  • dtype(Optional): The data type of the returned product. If not specified, the data type of the input array is used.
  • out(Optional): A location to store the result. If not specified, a new array is returned.
  • keepdims(Optional): If True, the reduced dimensions are retained as dimensions with size 1. This is useful for broadcasting purposes.

Note: If the input array is large or contains high-value elements, the dtype parameter can help prevent overflow by specifying a larger data type.

Example 1

Here’s how we can compute the product of all elements in a one-dimensional array:

import numpy as np
# Example array
arr = np.array([1, 2, 3, 4])
# Calculate product
result = np.prod(arr)
print(result)

The output will be as follows:

24

Example 2

For multidimensional arrays, .prod() can compute the product along rows or columns using the axis parameter:

import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Product along rows (axis 1)
row_product = np.prod(arr, axis=1)
print(row_product)
# Product along columns (axis 0)
col_product = np.prod(arr, axis=0)
print(col_product)

The output will be as follows:

[ 6 120]
[ 4 10 18]

The calculation will be like this:

  • Row-wise product: [1×2×3, 4×5×6] = [6, 120]
  • Column-wise product: [1×4, 2×5, 3×6] = [4, 10, 18]

Example 3

The dtype parameter can handle scenarios where array values are large, preventing overflow by converting results to a larger data type as follows:

import numpy as np
arr = np.array([1000, 2000, 3000], dtype=np.int32)
# Default dtype (may overflow)
default_result = np.prod(arr)
# Specifying dtype to prevent overflow
safe_result = np.prod(arr, dtype=np.int64)
print("Default:", default_result)
print("Safe:", safe_result)

The output will be as follows:

Default: 1294967296
Safe: 6000000000

Codebyte Example

The below example shows how .prod() can calculate the total items sold for each department or across all days, as follows:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy