.prod()
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): IfTrue
, 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 arrayarr = np.array([1, 2, 3, 4])# Calculate productresult = 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 nparr = 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 nparr = np.array([1000, 2000, 3000], dtype=np.int32)# Default dtype (may overflow)default_result = np.prod(arr)# Specifying dtype to prevent overflowsafe_result = np.prod(arr, dtype=np.int64)print("Default:", default_result)print("Safe:", safe_result)
The output will be as follows:
Default: 1294967296Safe: 6000000000
Codebyte Example
The below example shows how .prod()
can calculate the total items sold for each department or across all days, as follows:
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours