IKH

chapter 16

Matrix multiplication by using dot() function

  • For a given two ndarrays a,b, if we perform a*b ==> element level multiplication will be happened.
  • To perform matrix level multiplication we have to use dot() function.
  • dot() function in available in numpy module.
  • dot() method is present in ndarray class also.

Example

Python
# dot() function in numpy module
import numpy as np
help(np.dot)

Output

PowerShell
Help on function dot in module numpy:

dot(...)
    dot(a, b, out=None)

Example

Python
# dot() method in ndarray class
help(np.ndarray.dot)

Output

PowerShell
Help on method_descriptor:

dot(...)
    a.dot(b, out=None)
    
    Dot product of two arrays.

figar banana

Example

Python
# Element level multiplication
a = np.array([[10,20],[30,40]])
b = np.array([[1,2],[3,4]])
print(f"array a : \n {a}")
print(f"array b : \n {b}")
print("Element level multiplication ")
ele_multiplication = a* b
print(f"array b : \n {ele_multiplication}")

Output

PowerShell
array a :
 [[10 20]
 [30 40]]
array b :
 [[1 2]
 [3 4]]
Element level multiplication
array b :
 [[ 10 40]
 [ 90 160]]

Figar banana

Example

Python
# matrix multiplication using dot() function in numpy module
a = np.array([[10,20],[30,40]])
b = np.array([[1,2],[3,4]])
dot_product = np.dot(a,b)
print(f"array a : \n {a}")
print(f"array b : \n {b}")
print(f"Matrix multiplication:\n {dot_product} ")

Output

PowerShell
array a :
 [[10 20]
 [30 40]]
array b :
 [[1 2]
 [3 4]]
Matrix multiplication:
 [[ 70 100]
 [150 220]]

Example

Python
# matrix multiplication using dot() method in ndarray class
a = np.array([[10,20],[30,40]])
b = np.array([[1,2],[3,4]])

Output

PowerShell
output nhi h

Example

Python
dot_product = a.dot(b)
print(f"array a : \n {a}")
print(f"array b : \n {b}")
print(f"Matrix multiplication:\n {dot_product} ")

Output

PowerShell
array a :
 [[10 20]
 [30 40]]
array b :
 [[1 2]
 [3 4]]
Matrix multiplication:
 [[ 70 100]
 [150 220]]