Open In App

Matrix Multiplication in NumPy

Last Updated : 02 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Let us see how to compute matrix multiplication with NumPy. We will be using the numpy.dot() method to find the product of 2 matrices.

For example, for two matrices A and B.
A = [[1, 2], [2, 3]]
B = [[4, 5], [6, 7]]

So, A.B = [[1*4 + 2*6, 2*4 + 3*6], [1*5 + 2*7, 2*5 + 3*7]
So the computed answer will be: [[16, 26], [19, 31]]

In Python numpy.dot() method is used to calculate the dot product between two arrays.

Example 1 : Matrix multiplication of 2 square matrices.




# importing the module
import numpy as np
  
# creating two matrices
p = [[1, 2], [2, 3]]
q = [[4, 5], [6, 7]]
print("Matrix p :")
print(p)
print("Matrix q :")
print(q)
  
# computing product
result = np.dot(p, q)
  
# printing the result
print("The matrix multiplication is :")
print(result)


Output :

Matrix p :
[[1, 2], [2, 3]]
Matrix q :
[[4, 5], [6, 7]]
The matrix multiplication is :
[[16 19]
 [26 31]]

Example 2 : Matrix multiplication of 2 rectangular matrices.




# importing the module
import numpy as np
  
# creating two matrices
p = [[1, 2], [2, 3], [4, 5]]
q = [[4, 5, 1], [6, 7, 2]]
print("Matrix p :")
print(p)
print("Matrix q :")
print(q)
  
# computing product
result = np.dot(p, q)
  
# printing the result
print("The matrix multiplication is :")
print(result)


Output :

Matrix p :
[[1, 2], [2, 3], [4, 5]]
Matrix q :
[[4, 5, 1], [6, 7, 2]]
The matrix multiplication is :
[[16 19  5]
 [26 31  8]
 [46 55 14]]


Previous Article
Next Article

Similar Reads

Parallel matrix-vector multiplication in NumPy
In this article, we will discuss how to do matrix-vector multiplication in NumPy. Matrix multiplication with Vector For a matrix-vector multiplication, there are certain important points: The end product of a matrix-vector multiplication is a vector.Each element of this vector is obtained by performing a dot product between each row of the matrix a
2 min read
NumPy - 3D matrix multiplication
Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. What is 3D Matrix Multiplication?A 3D matrix is nothing but a collection (or a stack) of many 2D matrices, just like how a 2D m
2 min read
Python | Numpy numpy.matrix.any()
With the help of Numpy numpy.matrix.any() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison if any of the element matches it return true. Syntax : numpy.matrix.any() Return : Return true if any match found else false Example #1 : In this example we can see
1 min read
Python | Numpy numpy.matrix.all()
With the help of Numpy numpy.matrix.all() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison. Syntax : numpy.matrix.all() Return : Return true if found match else false Example #1 : In this example we can see that with the help of matrix.all() method, we are
1 min read
Python | Numpy numpy.matrix.A()
With the help of Numpy numpy.matrix.A() method, we can get the same matrix as self. It means through this method we can get the identical matrix. Syntax : numpy.matrix.A() Return : Return self matrix Example #1 : In this example we can see that with the help of matrix.A() method, we are able to get the self matrix. # import the important module in
1 min read
Python | Numpy numpy.matrix.T()
With the help of Numpy numpy.matrix.T() method, we can make a Transpose of any matrix either having dimension one or more than more. Syntax : numpy.matrix.T() Return : Return transpose of every matrix Example #1 : In this example we can see that with the help of matrix.T() method, we are able to transform any type of matrix. # import the important
1 min read
Python | Numpy numpy.matrix.H()
With the help of Numpy numpy.matrix.H() method, we can make a conjugate Transpose of any complex matrix either having dimension one or more than more. Syntax : numpy.matrix.H() Return : Return conjugate transpose of every complex matrix Example #1 : In this example we can see that with the help of matrix.H() method, we are able to transform any typ
1 min read
Difference between Numpy array and Numpy matrix
While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using numpy.array() function. It is used to convert a l
3 min read
NumPy | Vector Multiplication
Vector multiplication is of three types: Scalar Product Dot Product Cross Product Scalar Multiplication: Scalar multiplication can be represented by multiplying a scalar quantity by all the elements in the vector matrix. Code: Python code explaining Scalar Multiplication # importing libraries import numpy as np import matplotlib.pyplot as plt impor
1 min read
Multiplication of two Matrices in Single line using Numpy in Python
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 matr
3 min read