Change language

Multiplying two matrices in one line using Numpy in Python

| | | | |

Matrix multiplication — it is an operation that takes two matrices as input and creates one matrix by multiplying the rows of the first matrix by the column of the second matrix. In matrix multiplication, make sure that the number of rows in the first matrix must be equal to the number of columns in the second matrix. 
Example: Multiplying two 3 × 3 matrices by each other

 Input: matrix1 = ([1, 2, 3], [3, 4, 5 ], [7, 6, 4]) matrix2 = ([5, 2, 6], [5, 6, 7], [7, 6, 4]) Output: [[36 32 32] [70 60 66] [93 74 100]] 

Methods for multiplying two matrices in python
1. Using an explicit for: loop is simple a matrix multiplication method, but one of the expensive methods for a large set of inputs. In this we use nested for loops to iterate over each row and each column. 
If Matrix1 matrix is ​​ pCht and matrix2 is matrix MXL.

# enter two matrices nxm

matrix1 = [[ 12 , 7 , 3 ],

  [ 4 , 5 , 6 ],

[ 7 , 8 , 9 ]]

matrix2 = [[ 5 , 8 , 1 ],

  [ 6 , 7 , 3 ],

[ 4 , 5 , 9 ]]

 

res = [[ 0 for x in range ( 3 )] for y in range ( 3 )] 

 
# explicit for loops

for i in range ( len (matrix1)):

  for j in range ( len (matrix2 [ 0 ])):

  for k in range ( len (matrix2)):

 

# given matrix

res [i] [j] + = matrix1 [i] [k] * matrix2 [k] [j]

 

print (res)

Output:

 [[114 160 60] [74 97 73] [119 157 112]] 

In this program we used nested for loops to compute a result that will iterate over all the rows and columns of the matrices, and finally accumulate the sum of the result as a result. 
2. Using Numpy: Multiplication using Numpy is also known as vectorization, the main purpose of which is to reduce or eliminate the explicit use of for loops in the program, which makes calculations faster. 
Numpy — it is building a package in python to handle and manipulate arrays. For large matrix operations, we use the numpy python package, which is 1000 times faster than a single iterative method. 
For more information on Numpy, please visit the link

# We need to install NumPy in order to import it

import numpy as np

 
# enter two matrices

mat1 = ([ 1 , 6 , 5 ], [ 3 , 4 , 8 ], [ 2 , 12 , 3 ] )

mat2 = ([ 3 , 4 , 6 ], [ 5 , 6 , 7 ], [ 6 , 56 , 7 ])

 
# This will return a dot product

res = np.dot (mat1, mat2)

  
# printing the given matrix

print (res)

Output:

 [[63 320 83] [77 484 102] [84 248 117]] 

In the above example, we used dot product, and in mathematics, dot product — it is an algebraic operation that takes two vectors of the same size and returns one number. The result is calculated by multiplying the respective entries and adding these products.

This article courtesy of Diraj Sharma . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method