Products – NumPy uFuncs (Python Tutorial)

Products NumPy UFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Products Universal Functions. Learn the usage of these functions with the help of examples.

NumPy Products

In the process of finding the product of a certain set of values, we are multiplying them with each other and what we receive at the end is the product. In order to find the product of elements in an array we can use the prod() function.

Let us take an example:

#now first of all we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to find product for
a=np.array([1, 3, 5, 7])
#we will use the cumsum() function here
c=np.prod(a)
#now we will print the array
print(c)

Output.

105

Here in this example, we are getting the product of the values present in the array. Now we will take another example where we will take two arrays to find their product.

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to product for
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
#we will use the prod() function here
c=np.prod([a, b])
#now we will print the array
print(c)

Output.

40320

here in this example, we see we are getting such big value because it is multiplying both the arrays as in 1*3*5*7*2*4*6*8 = 40320.

Product Over an Axis

Now in order to get the product of arrays individually, we will use the axis. If we specify the axis to be one, then it will give us a product of each array as a result.

let us take an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to product for
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
#we will use the prod() function here
c=np.prod([a, b],axis=1)
#now we will print the array
print(c)

Output.

[105 384]

here in this example, we are getting the product of each array individually.

Cumulative Product

In order to take the product of the elements in the array partially, we will use the cumulative product. As a result of this product, every value will be the product of the value itself and the values behind it. We will use the cumprod() function of this product.

let us take an example to understand in a better manner:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to find product for
a=np.array([1, 3, 5, 7])
#we will use the cumprod() function here
c=np.cumprod(a)
#now we will print the array
print(c)

Output.

[ 1 3 15 105]

Here in this example, we see at the end we get the partial product of the elements in the array. Let take another example where we will use the axis.

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to product for
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
#we will use the cumprod() function here
c=np.cumprod([a, b],axis=1)
#now we will print the array
print(c)

Output.

[[ 1 3 15 105]
[ 2 8 48 384]]

So here we get the cumulative product of every array with axis=1 which gives us product of each array individually.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *