Machine Learning Prerequisites (Numpy)

You are currently auditing this course.
24 / 32

Numpy - Mathematical Operations on NumPy Arrays - Multiplication and Dot Product

In this chapter, we will discuss the Multiplication and Dot Product of two NumPy arrays.

Multiplication

Multiplication between two NumPy arrays is an element-wise product, and is represented by '*'

e.g.

import numpy as np
A = np.array( [ [ 1,1], [0, 1] ] )
B = np.array( [ [2, 0], [3, 4] ] )

Please note, this element-wise product '*' is different from 'dot product '.' of matrices.

'dot product' is also known as 'matrix product'.

M = A * B 

print(A)
array([ [1, 1],
        [0, 1] ])

print(B)
array( [ [2, 0],
         [3, 4] ] )

print(M)

Output (M) will be

array([ [2, 0 ],
        [0, 4] ] )

Above, element-wise product(M) is computed as below

M = [  [ A[1, 1] * B[1, 1], A[1, 2] * B[1, 2] ],
       [ A[2, 1] * B[2, 1], A[2, 2] * B[2, 2] ]  ]
     = [  [ (1 * 2), (1 * 0)],
          [ (0 * 3), (1, 4) ]  ]

M = [ [2, 0],
      [0, 4] ]

Matrix Product (or Dot Product)

Matrix Product (or Dot Product) between two NumPy is different from the element-wise product (or Multiplication). The dot product is represented by '.'

e.g.

import numpy as np
C = np.array( [ [ 1,1], [0, 1] ] )
D = np.array( [ [2, 0], [3, 4] ] )

Please note, this dot product '.' or 'matrix product' is different from 'element-wise' product '*'

P = np.dot(C, D) 

print(C)
array([ [1, 1],
        [0, 1] ])

print(D)
array( [ [2, 0],
         [3, 4] ] )

print(P)

Output (P) will be

array([ [5, 4 ],
        [ 3, 4] ] )

Above, element-wise product(P) is computed as below

P = [  [  [( (C[1,1] * D[1, 1]) + (C[1, 2] * D[2, 1] ) )],  [( (C[1,1] * D[1, 2]) + (C[1, 2] * D[2, 2] ) )]  ],
       [  [( (C[2,1] * D[1, 1]) + (C[2, 2] * D[2, 1] ) )],  [( (C[2,1] * D[1, 2]) + (C[2, 2] * D[2, 2] ) )]  ]  ] 
     = [  [ ( (1 * 2) + (1 * 3) ),   ( (1 * 0) + (1 * 4) ) ],
          [ ( (0 * 2) + (1 * 3) ),   ( (0 * 2) + (1 * 4) )  ]   ]
     = [ [ (2 + 3),  (0 + 4) ],
         [ (0 + 3),  (0 + 4) ] ]
     = [ [5, 4],
         [3, 4] ]
INSTRUCTIONS

Please follow the below steps:

(1) Please import required libraries

 import numpy as np

Multiplication (element-wise product)

(2) Create two NumPy arrays - A_arr and B_arr - as shown below

A_arr = np.array([ [ 5,9], [4, 7] ])
B_arr = np.array( [ [2, 8], [1, 6] ] )

(3) Multiply the above two NumPy arrays (A_arr and B_arr) and store the result in a variable M_arr

<<your code comes here>> = A_arr  << your code comes here>> B_arr

(4) Print the array M_arr to see its values

print(<<your code comes here>>)

Dot Product (Matrix Product)

(1) Create two NumPy arrays - C_arr and D_arr - as shown below

C_arr = np.array([ [ 5,9], [4, 7] ])
D_arr = np.array( [ [2, 8], [1, 6] ] )

(2) Perform dot product of the above two NumPy arrays (C_arr and D_arr) and store the result in a variable P_arr

<<your code comes here>> = np.dot(<<your code comes here>>)

(3) Print the array P_arr to see its values

print(<<your code comes here>>)
See Answer

No hints are availble for this assesment

Loading comments...