Multiply two matrices in python numpy

Matrix multiplication is an operation that takes two matrices as input and produces single matrix by multiplying rows of the first matrix to the column of the second matrix.In matrix multiplication make sure that the number of columns of the first matrix should be equal to the number of rows of the second matrix. 

Example: Multiplication of two matrices by each other of size 3×3. 

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 to multiply two matrices in python 

1.Using explicit for loops: This is a simple technique to multiply matrices but one of the expensive method for larger input data set.In this, we use nested for loops to iterate each row and each column. 

If matrix1 is a n x m matrix and matrix2 is a m x l matrix.

Implementation:

Python3

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)]

for i in range(len(matrix1)):

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

        for k in range(len(matrix2)):

            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 have used nested for loops for computation of result which will iterate through each row and column of the matrices, at last it will accumulate the sum of product in the result. 

2. Using Numpy : Multiplication using Numpy also know as vectorization which main aim to reduce or remove the explicit use of for loops in the program by which computation becomes faster. 
Numpy is a build in a package in python for array-processing and manipulation.For larger matrix operations we use numpy python package which is 1000 times faster than iterative one method. 
For detail about Numpy please visit the Link

Implementation:

Python3

import numpy as np

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

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

res = np.dot(mat1,mat2)

print(res)

Output: 
 

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

Using numpy 

Python3

import numpy as np

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

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

res = mat1 @ mat2

print(res)

Output: 
 

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

In the above example we have used dot product and in mathematics the dot product is an algebraic operation that takes two vectors of equal size and returns a single number. The result is calculated by multiplying corresponding entries and adding up those products.

This article is contributed by Dheeraj Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. 


How do you multiply matrices in Python NumPy?

There are three main ways to perform NumPy matrix multiplication:.
dot(array a, array b) : returns the scalar or dot product of two arrays..
matmul(array a, array b) : returns the matrix product of two arrays..
multiply(array a, array b) : returns the element-wise matrix multiplication of two arrays..

Which function multiply two matrices in NumPy?

To multiply two matrices use the dot() function of NumPy. It takes only 2 arguments and returns the product of two matrices.

How do you multiply matrix in Python?

In Python, @ is a binary operator used for matrix multiplication. It operates on two matrices, and in general, N-dimensional NumPy arrays, and returns the product matrix.

How do you multiply a 2x2 matrix in Python?

Step1: input two matrix. Step 2: nested for loops to iterate through each row and each column. Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix with each elements of second matrix, then add all multiplied value.