NumPy Product
Submitted by moazkhan on Thursday, July 16, 2020 - 10:19.
In this tutorial you will learn:
- What is the product of numbers?
- Implementation of product using different functions in Python
- Python Syntax
Product of Number
Product is just another name for the operation of multiplication. It is a very common operation in the world of computation, mathematics and science. Product is taken of more than one elements which are contained in data structures like list, tuple, array etcImplementation of Product
Function prod() is used to get the product of all the elements of the array, the function takes in an array as an only mandatory parameter and returns a number as output. The result of this example will be 1644 (4 x 8 x 12 x 16)- #importing the NumPy library
- import numpy as np
- #declaring an array
- my_arr= np.array([4,8,12,16])
- print('Array of which product is being taken:', my_arr)
- #finding the product of contents of an array
- res = np.prod(my_arr)
- #printing the result
- print('The result of prod() function:', res)
- #importing the NumPy library
- import numpy as np
- #declaring three arrays
- my_arr1= np.array([1,3,5,7])
- my_arr2=np.array([2,4,6,8])
- my_arr3=np.array([3,6,9,12])
- print('Three Arrays of which product is being taken:', my_arr1, my_arr2, my_arr3)
- #finding the product of contents of 3 arrays separately
- res = np.prod([my_arr1,my_arr2, my_arr3], axis=1)
- #printing the result
- print('The result of prod() function with axis = 1:', res)
- #importing the NumPy library
- import numpy as np
- #declaring an array
- arr= np.array([10,20,30,40])
- print('Array of which cummulative product is being taken:', arr)
- #taking the cummulative product of the contents of an array
- res = np.cumprod(arr)
- #printing the result
- print('The result of cumprod() function:', res)
Add new comment
- 80 views