لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy ufunc Products

We use the prod() function to find the product of the elements in an array.

Example 1: Find the product of the elements of this array.

Code

import numpy as np

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

x = np.prod(arr)

print(x)

the output will be

120

Returns: 120 since 1*2*3*4*5 = 120

Example 2: Find the product of the elements of two arrays.

Code

import numpy as np

arr1 = np.array([1, 2, 3,])
arr2 = np.array([5, 6, 7,])

x = np.prod([arr1, arr2])

print(x)

the output will be

1260

Returns: 1260 1*2*3*5*6*7 = 1260 Product Over an Axis

If you specify axis=1, NumPy will return the product of each array.

Example 3: Perform summation in the following array over 1st axis.

Code

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([5, 6, 7])

newarr = np.prod([arr1, arr2], axis=1)

print(newarr)

the output will be

[6 210]

Cummulative Product

Cummulative product means taking the product partially.

E.g. The partial product of [1, 2, 3] is [1, 1*2, 1*2*3] = [1, 2, 6]

We perfom partial sum with the cumprod() function.

Example 4: Take cummulative product of all elements for following array.

Code

import numpy as np

arr = np.array([5, 6, 7])

newarr = np.cumprod(arr)

print(newarr)

the output will be

[ 5 30 210]
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.