Multiplication of two matrices in Python using NumPy

In this Python tutorial, we will learn how to perform multiplication of two matrices in Python using NumPy.

Python is a programming language in addition that lets you work quickly and integrate systems more efficiently.

However, In this tutorial, we will be solving multiplication of two matrices in the Python programming language.

What is Numpy?

Numpy is an array-processing library. It provides a high-performance multidimensional array function and tools for working with these arrays. It is the fundamental library for machine learning computing with Python.
Numpy can also be used as an efficient multi-dimensional container of data.

for more information visit numpy documentation

Matrix Multiplication in Python

in this tutorial, we will see two segments to solve matrix

  • nested loop
  • using Numpy array

Here is the full tutorial of multiplication of two matrices using a nested loop: Multiplying two matrices in Python



Multiplication of two matrices X and Y is defined only if the number of columns in X is equal to the number of rows Y.or else it will lead to an error in the output result.

If X is a (n X m) matrix and Y is a (m x 1) matrix then, XY is defined and has the dimension (n x 1).

follow the given below code to implement matrics operation between two matrices.

#using nested loop
# 3x3 matrix
X = [[1,3,2],
    [3 ,6,8],
    [5 ,2,1]]
# 3x3 matrix
Y = [[6,8,6],
    [6,3,3],
    [2,5,1]]
# result is 3x4
result = [[0,0,0],
         [0,0,0],
         [0,0,0]]
# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]
for r in result:
   print(r)

The output will be

[28, 27, 17]
[70, 82, 44]
[44, 51, 37]

now we will focus on part two of this tutorial which is :

Matrix Multiplication in Python Using Numpy array

Numpy makes the task more simple. because Numpy already contains a pre-built function to multiply two given parameter which is dot() function

we will encode the same example as mentioned above

before it is highly recommended to see How to import libraries for deep learning model in python ?

import numpy as np 
  
# input two matrices 
mat1 = ([1,3,2],[3 ,6,8],[5 ,2,1]) 
mat2 = ([6,8,6],[6,3,3],[2,5,1]) 
  
# This will return dot product 
result = np.dot(mat1,mat2) 
  
# print resulted matrix 
print(result)

The output:

[28, 27, 17]
[70, 82, 44]
[44, 51, 37]

congratulation, you have completed and learn how to multiply two matrices using Numpy array

Conclusion

In this tutorial, we have learned the following

  • what is python
  • multiplication of two matrices-using nested loop
  • multiplication of two matrices-using Numpy array
  • implementation in python script

Hope you got a fair idea about the multiplication of the matrix.

Leave a Reply

Your email address will not be published. Required fields are marked *