Axis-wise aggregation
Reduce a multidimensional tensor along one or more dimensions.
NumPy · Function
Returns the product of array elements over a selected axis or axes. It can reduce all elements, preserve reduced dimensions, control accumulation dtype, mask inputs, and write into a supplied output array.
Pass array-like data to compute a product over all elements or selected axes. The result can use a chosen accumulator dtype, write into an output array, preserve reduced dimensions, include an initial factor, and restrict participation with a boolean mask.
numpy.prod(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)| Name | Type | Description |
|---|---|---|
a | array_like | Input data. |
axis | None or int or tuple of ints, optional | Axis or axes along which a product is performed; defaults to all elements. |
dtype | dtype, optional | Type of the returned array and multiplication accumulator. |
out | ndarray, optional | Array in which to place the result. |
keepdims | bool, optional | If true, reduced axes are retained with size one. |
initial | scalar, optional | Initial value used in the product. |
where | array_like of bool, optional | Boolean selection of elements included in the product. |
product_along_axis: ndarray; an array shaped like the input with specified axes removed, or a reference to out when out is supplied.
import numpy as np
values = np.array([[1, 2, 3], [4, 5, 6]])
print(np.prod(values))
print(np.prod(values, axis=0))
print(np.prod(values, axis=1, keepdims=True))Computes the product of all elements, then products along columns and rows while retaining the reduced dimension.
Reduce a multidimensional tensor along one or more dimensions.
Calculate a product while retaining singleton dimensions for broadcasting.
Exclude selected values from a reduction using a boolean condition.
Control integer accumulation or reuse preallocated storage.
np.prod multiplies array elements over the requested axis or axes. With axis=None, it reduces all elements; with an axis, the reduced dimension is removed unless keepdims is enabled.
Follow-up: How would you preserve broadcasting compatibility after reducing? Use keepdims=True so reduced axes remain with length one.
dtype controls both the returned array type and the accumulator used during multiplication. Choosing it explicitly can control integer precision and the result representation.
Follow-up: Why can dtype matter for integer inputs? An unspecified integer dtype can use a platform-dependent default, affecting overflow behavior and portability.
where specifies which elements participate in the product, and initial supplies an initial value for the multiplication. These options allow masked reductions and an extra multiplicative factor without first modifying the input.
Follow-up: What should be checked when using out? Its shape must match the expected reduced result, and the result may be cast to its dtype.
numpy.sumnumpy.cumprodnumpy.multiply.reduce