NumPy Prod – A Complete Guide

NumPy Prod Cover Image

Hello and welcome to this tutorial on Numpy prod. In this tutorial, we will be learning about the NumPy prod() method and also seeing a lot of examples regarding the same. So let us begin!

Also read: NumPy Zeros – A Complete Guide


What is NumPy prod?

The prod method in NumPy is a function that returns the product of the array elements. It can be the product of all the array elements, the product of the array elements along the rows, or the product of the array elements along the columns. 
We will see the examples for each of these in the upcoming section of this tutorial.


Syntax of NumPy prod

Let us first have a look at the syntax of the NumPy prod function.

numpy.prod(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
ParameterDescriptionRequired/Optional
a (array_like)Input data.Required
axisAxis along which the product of the array is to be calculated. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that the product of the entire array is to be returned. If axis is a tuple of ints, a product is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.Optional
dtype (data type)The data type of the array to be returned.Optional
outAn alternative output array in which to place the result. It must have the same shape as the expected output.Optional
keepdims (bool)If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.Optional
initialStarting value for the product.Optional
whereElements to include in the product.Optional

Returns:
An array with the same shape as a which contains the product of the elements of a along the axis given and the specified axis removed. If the axis=None, a scalar is returned which is the product of the whole array.


Examples of numpy.prod()

Let’s get right into the different examples of using numpy.prod() function.

Product of the entire array

1-dimensional array

import numpy as np
 
a = [5, 3, 1]
 
product = np.prod(a)
print("a =", a)
print("Product of the array =", product)

Output:

a = [5, 3, 1]
Product of the array = 15

Product of the array = 5*3*1 = 15

2-dimensional array

import numpy as np
 
a = [[5, 3, 1], [1, 2, 4]]
 
product = np.prod(a)
print("a =", a)
print("Product of the array =", product)

Output:

a = [[5, 3, 1], [1, 2, 4]]
Product of the array = 120

Product of the array = 5*3*1*1*2*4 = 120


Product along the axis

Column-wise product

import numpy as np

a = [[5, 3, 1], 
     [1, 2, 4]]

# product along axis=0 i.e. columns
product = np.sum(a, axis=0)
print("a =", a)
print("Product of the array along the columns =", product)

Output:

a = [[5, 3, 1], [1, 2, 4]]
Product of the array along the columns = [5 6 4]

Column 0 product = 5*1 = 5
Column 1 product= 3*2 = 6
Column 2 product = 1*4 = 4

Row-wise product

import numpy as np

a = [[5, 3, 1], 
     [1, 2, 4]]

# product along axis=1 i.e. rows
product = np.prod(a, axis=1)
print("a =", a)
print("Product of the array along the rows =", product)

Output:

a = [[5, 3, 1], [1, 2, 4]]
Product of the array along the rows = [15  8]

Row 0 product = 5*3*1 = 15
Row 1 product = 1*2*4 = 8


Return the product of the array as float data type

This is the same as the above examples except that here the returned values are of float data type.

Product of the entire array

import numpy as np

a = [[2, 3 ,6], 
     [1, 5, 4]]

product = np.prod(a, dtype=float)
print("a =", a)
print("Product of the array along the columns =", product)

Output:

a = [[2, 3, 6], [1, 5, 4]]
Product of the array along the columns = 720.0

Column-wise product

import numpy as np

a = [[2, 3 ,6], 
     [1, 5, 4]]

# product along axis=0 i.e. columns
product = np.prod(a, axis=0, dtype=float)
print("a =", a)
print("Product of the array along the columns =", product)

Output:

a = [[2, 3, 6], [1, 5, 4]]
Product of the array along the columns = [ 2. 15. 24.]

Row-wise product

import numpy as np

a = [[2, 3 ,6], 
     [1, 5, 4]]

# product along axis=1 i.e. rows
product = np.prod(a, axis=1, dtype=float)
print("a =", a)
print("Product of the array along the rows =", product)

Output:

a = [[2, 3, 6], [1, 5, 4]]
Product of the array along the rows = [36. 20.]

Product of only specific elements in the array

1-dimensional array

import numpy as np

a = [2, 9, 3, 4, 1]

product = np.prod(a, where=[True, False, False, True, True])
print("a =", a)
print("Product of the array =", product)

Output:

a = [2, 9, 3, 4, 1]
Product of the array = 8

In the above code, the ‘where’ clause specifies which elements to include in the product. ‘True’ implies that include this value in the product and ‘False’ implies that do not include this value in the product calculation.
Here, where=[True, False, False, True, True] denotes that only include the elements at positions 0, 3 and 4 of the array in the product. Hence, product = a[0]*a[3]*a[4] = 2*4*1 = 8.

2-dimensional array

import numpy as np

a = [[2, 9], 
    [7, 10]]

product = np.prod(a, where=[[True, False], [False, True]])
print("a =", a)
print("Product of the array =", product)

Output:

a = [[2, 9], [7, 10]]
Product of the array = 20

Here, from the 0th row, only a[0][0] i.e. 2 and from the 1st row, only a[1][1] i.e. 10 is included in the product. Hence, product = 2*10 = 20.


Conclusion

That’s all! In this tutorial, we learned about the Numpy prod method and practiced different types of examples using the same.


Reference