Multiplication of two Matrices using Numpy in Python


In this tutorial, we are going to learn how to multiply two matrices using the NumPy library in Python. It's straightforward with the NumPy library.

It has a method called dot for the matric multiplication. You can install the NumPy library with the following command.

pip install numpy

Let's see the steps involved in the program.

  • Import the NumPy library.

  • Initialize the matrices.

  • Multiply the matrices with numpy.dot(matrix_1, matrix_2) method and store the result in a variable.

  • Print the result.

See the below code.

Example

# importing the module
import numpy
# initializing the matrices matrix_1 = [
      [1, 2, 3], [4, 5, 6], [7, 8, 9]
   ] matrix_2 = [
      [7, 8, 9], [4, 5, 6],[1, 2, 3]
   ]
# multiplying the two matrices
result = numpy.dot(matrix1, matrix2)
# printing the result
print(result)

Output

If you execute the above program, you will get the following results.

[[ 18 24 30]
[ 54 69 84]
[ 90 114 138]]

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 12-Feb-2020

691 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements