Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.prod

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.

How it works

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.

Syntax

numpy.prod(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)

Parameters

NameTypeDescription
aarray_likeInput data.
axisNone or int or tuple of ints, optionalAxis or axes along which a product is performed; defaults to all elements.
dtypedtype, optionalType of the returned array and multiplication accumulator.
outndarray, optionalArray in which to place the result.
keepdimsbool, optionalIf true, reduced axes are retained with size one.
initialscalar, optionalInitial value used in the product.
wherearray_like of bool, optionalBoolean selection of elements included in the product.

Returns

product_along_axis: ndarray; an array shaped like the input with specified axes removed, or a reference to out when out is supplied.

Key points

  • np.prod reduces array elements by multiplication along a chosen axis.
  • Negative axis values address dimensions from the trailing axis backward.
  • A tuple of axes lets one call reduce multiple dimensions together.
  • dtype determines both the output type and the type used for accumulation.
  • out lets the operation store its result in a caller-provided array.
  • The returned array keeps the non-reduced dimensions and removes reduced axes by default.

Common mistakes

  • Leaving axis as None reduces every element, rather than performing separate products along an axis.
  • When supplying out, allocate it with the shape expected after reduction; NumPy may cast values to its dtype.

Example: Products over array axes

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.

When to use it

Axis-wise aggregation

Reduce a multidimensional tensor along one or more dimensions.

Broadcast-compatible reduction

Calculate a product while retaining singleton dimensions for broadcasting.

Masked products

Exclude selected values from a reduction using a boolean condition.

Explicit dtype and output management

Control integer accumulation or reuse preallocated storage.

Interview questions

QWhat does np.prod compute, and how does axis affect the result?

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.

QWhat is the role of dtype in np.prod?

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.

QHow do where, initial, and out extend np.prod?

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.sum
  • numpy.cumprod
  • numpy.multiply.reduce

Source: numpy.prod. Checked 2026-10-15.