Python

# NumPy Matrix Multiplication

This tutorial is related to matrix multiplication with the help of the NumPy library. Matrix multiplication refers to how we can perform the multiplication operations on a matrix in a Python application. All the numerical and scientific operations are easily handled with the help of the NumPy library in Python. On this platform, we will guide all the readers through this tutorial about matrix multiplication with the help of proper examples. We will go through different steps and sessions while maintaining and running the Python code on PyCharm, Spyder, or the Notebook Jupiter tool. Before installing NumPy, we will demonstrate the matrix multiplication in detail. Let’s start!

## NumPy Matrix Multiplication in Python

In Python, we will efficiently perform the matrix multiplication with the help of the NumPy methods. All methods are defined perfectly in the coming sections. As the name suggests, matrix multiplication, we only multiply the matrix to solve the mathematical problems. We can use the different functions for matrix multiplication in Python which is defined in the NumPy library. The syntax of all these methods is explained in the following section.

## Syntax of the Matrix Multiplication Method

Here, we will explain the syntax of matrix multiplications in different dimensions.

The syntax for a 2D matrix using the dot function is given as follows:

MM= numpy.dot (a, b)

Or

MM=a@b

Here, NumPy is a Python library and the variables “a” and “b” are the arrays on which we apply the multiplication. After that, we have another way to perform the matrix multiplication which is mentioned previously. We can use the “@” between two arrays to perform the multiplication and the syntax for a 3D matrix using the following method:

MM= numpy. matmul (a, b, c)

Or

MM = numpy. tensordot (a, b, axes)

We need three parameters here: “a”, “b”, and “axes”. Here, the “a” and “b” variables are two matrices, and the axes are also defined in the function. If the axes value is 0, it means that the matrices have the cross product.

## Install and Import the NumPy Library

Open the Python application. We create a new Python file where we import the NumPy library. But we need to install a NumPy library first.

Run the following command in the application terminal:

pip install numpy

We rename the file accordingly. Now, we import a NumPy library to perform the multiplication of arrays and alias the NumPy library as “np”.

import numpy as np

In this way, we install and import the NumPy library in our Python application. Now, let’s have some examples related to the matrix multiplication.

Example 1:
This is our first example in which we multiply a 2D matrix using the NumPy library method. The reference code of this example is mentioned in the following:

import numpy as np
arr1 = np.array([[2,7],[6,9]])
arr2 = np.array([[2,5],[4,8]])
res = np.dot(arr1,arr2)
print (res)

Here, we use the dot method for matrix multiplication. As illustrated previously, we initialize two matrices named “arr1” and “arr2” and pass these two matrices in the “dot” method by calling it through the NumPy library. We store the value that the dot method returns in the “res” variable. Lastly, we pass the “res” variable in the print statement to show the output on the screen.

The result that we get from the previous code is given in the following:

[[ 32  66]
[ 48 102]]

As we can see, the output is shown in one matrix after multiplication [ [ 32 66] [ 48 102 ] ].

Example 2:
The second instance is also linked to the 2D matrix in which only two arrays are involved. In this example, we make use of the “@” operator for matrix multiplication. The reference code of this example is attached in the following:

import numpy as np
arr1 = np. array([[6,3],[2,7]])
arr2 = np. array([[1,9],[4,3]])
res = arr1 @ arr2
print (res)

Here, we import the NumPy library. Then, we initialize the matrices and name them as “arr1” and “arr2” in our code. After that, we apply the “@” between two matrices for multiplication and store this multiplication value in the “res” variable. Lastly, we pass the “res” variable in the print statement to show the output on the console.

The output of this example is attached in the following:

[[18 63]
[30 39]]

The multiplication of two matrices is also a matrix [18 63] [30 39]].

Example 3:
This is another example of matrix multiplication. But in this case, we multiply more than two matrices. We use the NumPy library “matmul” method here. The reference code of multi-the dimensional matrices is attached as follows:

import numpy as np
arr1 = np. array([[4,7],[2,6]])
arr2 = np. array([[7,9],[1,3]])
arr3 = np. array ([[9,2],[5,8]])
output = np.matmul(arr1,arr2,arr3)
print (output)

Here, we initialize three matrices named “arr1”,”arr2”, and “arr3”. After that, we call the “matmul” method of the NumPy library and pass these three matrices in this method. We store the multiplication of matrices in the “output” variable. Lastly, we pass the “output” variable in the print statement to show the output on the console.

The output of these matrices’ multiplication is [[ 35 57] [20 36]] as mentioned in the following:

[[35 57]
[20 36]]

Example 4:
In this example, we will discuss the tensordot function for matrix multiplication. The reference code of this example is attached in the following:

import numpy as np
ar = np.array([[1, 2, 6], [3, 4, 8]])
br = np.array([[9, 4],[2, 2], [1, 2]])
d = np.tensordot(ar, br,axes=1)
print(d)

Here, we take two matrices named “ar” and “br”. After that, we call the “tensordot” method from the NumPy library in which we pass these two matrices and axes. Here, we declare a “d” variable to store the result of matrix matrices. Lastly, we pass the variable “d” in the print statement to show the result of matrix multiplication on a console. In this example, we multiply the matrices whose dimension is 3×2. In the first matrix, we have 2 rows and 3 columns. While we have 3 rows and 2 columns in the second matrix.

The output of the tensordot function is mentioned in the following. The result after the matrix multiplication is [ [ 19 20 ] [ 43 36 ] ] as you can see in the following:

[[19 20]
[43 36]]

## Conclusion

We conclude that the NumPy library methods are essential for matrix multiplication. Here, we used four different ways for matrix multiplication. We see that when two or three matrices are multiplied, at the end (after multiplication), only one matrix exists. Keep in mind that the np.matmul() and the np.tensordot() functions are used to perform the matrix multiplications on multi-dimensional arrays; their dimensions and shapes should match accordingly. Hopefully, all the illustrated examples are helpful for you. You can practice these examples in your Python application. 