codingstreets
Search
Close this search box.
python-numpy-products 

Introduction to Python NumPy Products

In This Article, You Will Learn About Python Numpy Products.

Python Numpy Products – Before moving ahead, let’s know a bit of Python Numpy Summations

Products

Product – It is the process of multiplying each element of the array one by one.

Example – Finding the product of the array.

import numpy as np

array_1 = np.array([11, 12, 13])

updated_array = np.prod(array_1)

print(updated_array)
Output - 

1716

As a result, it returns product (11*12*13) of each element of the array_1.

Example – Finding the product of two arrays.

import numpy as np

array_1 = np.array([2, 3, 4])
array_2 = np.array([1, 2, 3])

updated_array = np.prod([array_1, array_2])

print(updated_array)
Output - 

144

As a result, it returns product of both arrays.

Product through Axis

Numpy will product numbers of each array in itself, if we specify axis parameter.

Example – Summation in the both arrays with 1st axis:

import numpy as np

array_1 = np.array([2, 3, 4])
array_2 = np.array([1, 2, 3])

updated_array = np.sum([array_1, array_2], axis=1)

print(updated_array)
Output - 

[24 6]

As a result, it returns an array of product of each element of the array in itself.

Cummulative Product

Cummulative Sum means adding each element of array in itself one by one or step by step.

E.g. Sum of [1, 2, 3] one by one would be [1, 1*2, 1*2*3] = [1, 2, 6].

In Numpy, it performs with cumprod() function.

Example – Finding cummulative product of the array.

import numpy as np

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

updated_array = np.cumprod(array)

print(updated_array)
python-numpy-products

As shown above, it returns an array of product of each element of array.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on