Python numpy prod

The Python numpy prod() function calculates the product or multiplication of all the elements in a given array along the specified axis.

Syntax

The syntax of this statistical Python numpy prod() method is

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

Here, a is a ndarray that is mandatory, and all the remaining arguments are optional.

Python numpy prod Example

The product of an empty array returns element 1 as an output.

import numpy as np

a = np.array([])

b  = np.prod(a)
print(b)
1.0

In this example, we declared a one-dimensional ndarray. Next, it calculates the product of all the array items.

The initial argument is the starting value. Use this argument to add a specific value to the whole product or axis based.

import numpy as np

a = np.array([2, 4.5, 6, 8])

b  = np.prod(a)
print(b)

c = np.prod(a, initial = 10)
print(c)
432.0
4320.0

b = 2 * 4.5 * 6 * 8 = 432.0

c = 10 (initial) * 2 * 4.5 * 6 * 8 = 4320.0

The dtype argument helps to check the data type of output.

import numpy as np

a = np.array([1.2, 2.4, 3.6, 4.8], dtype = np.int8)

b  = np.prod(a)
print(b)

if np.prod(a).dtype == int:
    print('Integer')
else:
    print('Not')
24
Integer

Python numpy prod two-Dimensional example

In this program, we will declare a two-dimensional square matrix and find its product.

b = 2 * 3 * 4 * 5 = 120

c = 7 * 2 * 3 * 4 * 5 = 840

import numpy as np

a = np.array([[2, 3], [4, 5]])

b  = np.prod(a)
print(b)

c = np.prod(a, initial = 7)
print(c)
120
840

If the axis is not provided, this prod() method will calculate the product of all the dimensional elements. As shown in the above example, it has returned the product of the two-dimensional array.

However, we can find the row and column-wise product by specifying the axis value. For instance, in the below example, axis = 0 calculates the product for each column, and axis = 1 finds each row.

Column Wise

In this example, we used the random randint method to generate a two-dimensional array of 3 * 4 size. Here, the c variable finds the product for each column, and the d variable finds the total for each column multiplied by 2.

import numpy as np

a = np.random.randint(1, 12, size = (3, 4))
print(a)

b  = np.prod(a)
print(b)

c = np.prod(a, axis = 0)
print(c)

d = np.prod(a, axis = 0, initial = 2)
print(d)
[[ 3  7  2 10]
 [10 10  7  1]
 [ 1  5 10  6]]

88200000

[ 30 350 140  60]

[ 60 700 280 120]

Row Wise

Use the out argument to save the Python numpy prod() method result in an array. Remember, the variable size has to match the output. Here, 

  • axis = 1 will calculate the multiplication for each row in an array. 
  • axis = 1, initial = 3 – For each row, it will multiply the result with 3.
  • initial = 2, out = x – Saves the output in x variable.
  • The keepdims argument accepts boolean True or False, and the default value is false. The result will reshape if you use the keepdims argument and assigned True value.
import numpy as np

a = np.random.randint(1, 6, size = (3, 4))
print(a)

x = np.arange(3)

b  = np.prod(a)
print(b)

c = np.prod(a, axis = 1)
print(c)

d = np.prod(a, axis = 1, initial = 3)
print(d)

np.prod(a, axis = 1, initial = 2, out = x)
print(x)

e = np.prod(a, axis = 1,  keepdims = True)
print(e)
[[2 2 1 5]
 [1 5 5 1]
 [2 2 5 4]]

40000

[20 25 80]

[ 60  75 240]

[ 40  50 160]

[[20]
 [25]
 [80]]

where argument

The where argument omits the row or column from the multiplication process. In the below Python numpy prod code, we used the where argument along the axis.

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 2]])
print(a)

b  = np.prod(a)
print(b)

c = np.prod(a, where = [False, True, True])
print(c)

d = np.prod(a, where = [False, True, True], axis = 0)
print(d)

e = np.prod(a, where = [True, False, True], axis = 1)
print(e)
[[1 2 3]
 [4 5 6]
 [7 8 2]]

80640

2880

[ 1 80 36]

[ 3 24 14]

where = [False, True, True] – The first column is false, so it will find the product of 2 and 3 columns. The c variable = 2 * 5 * 8 * 3 * 6 * 2 = 2880.

where = [False, True, True], axis = 0 – For the first column, the product of an empty array is 1. The 2nd row product = 2 * 5 * 8, and the 3rd = 3 * 6 * 2.

where = [True, False, True], axis = 1 – While finding the row based total, it will omit the 2nd column values. 1st row = 1 * 3, 2nd row = 4 * 6, and 3rd row = 7 * 2