0
0
NumPydata~5 mins

np.prod() for product in NumPy

Choose your learning style9 modes available
Introduction

We use np.prod() to multiply all numbers in a list or array together. It helps find the total product easily.

Calculating the total growth factor from multiple growth rates.
Finding the product of all elements in a dataset, like multiplying probabilities.
Multiplying all values in a row or column of a table.
Computing the product of dimensions to find total size or volume.
When you want to quickly multiply many numbers without a loop.
Syntax
NumPy
numpy.prod(a, axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True)

a is the input array or list of numbers.

axis lets you multiply along rows or columns (like 'axis=0' for columns, 'axis=1' for rows).

Examples
Multiplies all numbers: 2 * 3 * 4 = 24.
NumPy
np.prod([2, 3, 4])
Multiplies down each column: [1*3, 2*4] = [3, 8].
NumPy
np.prod([[1, 2], [3, 4]], axis=0)
Multiplies across each row: [1*2, 3*4] = [2, 12].
NumPy
np.prod([[1, 2], [3, 4]], axis=1)
Sample Program

This code multiplies all numbers in a list, then multiplies numbers down columns and across rows in a 2D array.

NumPy
import numpy as np

# List of numbers
numbers = [5, 6, 2]
product_all = np.prod(numbers)
print('Product of all numbers:', product_all)

# 2D array example
arr = np.array([[2, 3], [4, 5]])
product_columns = np.prod(arr, axis=0)
product_rows = np.prod(arr, axis=1)
print('Product of each column:', product_columns)
print('Product of each row:', product_rows)
OutputSuccess
Important Notes

If you don't specify axis, it multiplies all elements in the whole array.

You can use dtype to control the type of the result, like float or int.

Summary

np.prod() multiplies all elements in an array or list.

You can multiply across the whole array or along rows/columns using axis.

It is a quick way to find the total product without writing loops.