• We just launched and are currently in beta. Join us as we build and grow the community.

NumPy Product

Bhaktey10

High Availability Architect
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
156
Likes
120
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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 etc
Implementation 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)
  1. #importing the NumPy library
  2. import

    numpy as

    np
  3. #declaring an array
  4. my_arr=

    np.array

    (

    [

    4

    ,

    8

    ,

    12

    ,

    16

    ]

    )
  5. print

    (

    'Array of which product is being taken:'

    ,

    my_arr)
  6. #finding the product of contents of an array
  7. res =

    np.prod

    (

    my_arr)
  8. #printing the result
  9. print

    (

    'The result of prod() function:'

    ,

    res)

The prod() function can return the result of each array separately if we define its parameter “axis” equal to one. Here we will be taking three arrays and will find the prod of three arrays separately.
  1. #importing the NumPy library
  2. import

    numpy as

    np
  3. #declaring three arrays
  4. my_arr1=

    np.array

    (

    [

    1

    ,

    3

    ,

    5

    ,

    7

    ]

    )
  5. my_arr2=

    np.array

    (

    [

    2

    ,

    4

    ,

    6

    ,

    8

    ]

    )
  6. my_arr3=

    np.array

    (

    [

    3

    ,

    6

    ,

    9

    ,

    12

    ]

    )
  7. print

    (

    'Three Arrays of which product is being taken:'

    ,

    my_arr1,

    my_arr2,

    my_arr3)
  8. #finding the product of contents of 3 arrays separately
  9. res =

    np.prod

    (

    [

    my_arr1,

    my_arr2,

    my_arr3]

    ,

    axis=

    1

    )
  10. #printing the result
  11. print

    (

    'The result of prod() function with axis = 1:'

    ,

    res)

NumPy provides another function ‘cumprod()’, the function takes in an array and at each index gives out a cumulative product after multiplying up all the elements of the lower indexes of the array.
  1. #importing the NumPy library
  2. import

    numpy as

    np
  3. #declaring an array
  4. arr=

    np.array

    (

    [

    10

    ,

    20

    ,

    30

    ,

    40

    ]

    )
  5. print

    (

    'Array of which cummulative product is being taken:'

    ,

    arr)
  6. #taking the cummulative product of the contents of an array
  7. res =

    np.cumprod

    (

    arr)
  8. #printing the result
  9. print

    (

    'The result of cumprod() function:'

    ,

    res)

Book traversal links for NumPy Product

  • ‹ NumPy Permutations
  • Up
  • NumPy Random ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top