How to do matrix multiplication with Numpy

In this post, we will learn how to perform matrix multiplication with Pythin in Numpy. In Pythn we have at least a few ways to do matrix multiplication. Here we will learn how to use the Numpy function matmul(), which short for Matrix Multiplication.

To use Numpy’s matmul() function, we simply pass in the two numpy arrays that we want to multiply as arguments. Note that we can not multiply all matrices. The input matrices or arrays must have compatible shapes. Two matrices are compatible for multiplication if the number of columns in the first matrix is the same as the number of rows in the second matrix. when the arguments are compatible, matmul() will return a new array/matrix as the result of the matrix multiplication.

Let us import Numpy.

import numpy as np

Example 1: Matrix multiplication with matmul()

Let us first consider the example of multiple two matrices of the same dimension. In this example, we will be creating two matrices of dimesion 2×2 and multiply them with matmul() function.

First, let us create the first matrix with dim 2×2.

a = np.arange(4).reshape(2,2)
a

array([[0, 1],
       [2, 3]])

Now the second matrix with the same dimension.

b = np.arange(2,6).reshape(2,2)
b

array([[2, 3],
       [4, 5]])

We can multiply these two matrices of dim 2×2 using Numpy’s matmul() function by providing them as arguments. We get

np.matmul(a,b)

array([[ 4,  5],
       [16, 21]])

Example 2: Matrix multiplication with matmul()

In the second example, we consider multiplication of two matrices of dimesion 3×3. As we can see multiplying two 3×3 matrices is possible as they compatible.

a = np.arange(9).reshape(3,3)
a

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b = np.arange(3,12).reshape(3,3)
b

array([[ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
np.matmul(a,b)
array([[ 24,  27,  30],
       [ 78,  90, 102],
       [132, 153, 174]])

Example 3: Matrix multiplication tow matrices of different dimensions with matmul()

So far, we have seen examples of multiplying matrices with same dimension. In this example, let us multiply two matrices of different, but with compatible shapes, using Numpy’s matmul().

To be compatible, the column size of first matrix should match with row size of the second matrix. For example, we can multiply a 4×3 matrix with 3×3 matrix.

Let us create a 4×3 matrix with Numpy’s arange() and reshape() function.

a = np.arange(12).reshape(4,3)
a

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

Here we create a 3×3 matrix

b = np.arange(9).reshape(3,3)
b

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

And the result of multipying a 4×3 matrix with 3×3 matrix is another 4×3 matrix.

np.matmul(a,b)

array([[ 15,  18,  21],
       [ 42,  54,  66],
       [ 69,  90, 111],
       [ 96, 126, 156]])

Also not that matrix multiplication is not commutative. This means that the order of the matrices while performing multiplication matters. Therefore, a x b is not the same as b *a.