How to Get the Inverse of a Matrix in Python using Numpy 


               


How to Get the Inverse of a Matrix in Python using Numpy



Python


In this article, we show how to get the inverse of a matrix in Python using the numpy module.

The inverse of a matrix is a matrix that when multiplied with the original matrix produces the identity matrix. The identity matrix is a square matrix in which all the elements of the principal (main) diagonal are ones and all other elements are zeros.

With Python's numpy module, we can compute the inverse of a matrix without having to know how to mathematically do so.

The numpy module has a simple .I attribute that computes the inverse of a matrix.

This is shown in the following code below.



So the first thing we must do is import the numpy module. We do so with the line, import numpy as np. The reason we put, as np, is so that we don't have to reference numpy each time; we can just use np.

We then create a variable called matrix1 and set it equal to, np.matrix([[8,2],[7,3]])

We then reference matrix1 and you can see that it produces the matrix that we pictured above.

It's a 2x2 matrix, 2 rows and 2 columns.

We then obtain the inverse of the matrix with the line, matrix1.T

The .I attribute obtains the inverse of a matrix.

Let's break down how to solve for this matrix mathematically to see whether Python computed the inverse matrix correctly (which it did).

When dealing with a 2x2 matrix, how we obtain the inverse of this matrix is swapping the 8 and 3 value and placing a negative sign (-) in front of the 2 and 7. We then divide everything by, 1/determinant.

Doing the math to determine the determinant of the matrix, we get, (8)(3)-(2)(7)= 10.

So the determinant is 10.

So we multiply each element in the array by 1/10. Doing so gives us matrix([[ 0.3, -0.2],[-0.7, 0.8]]) as the inverse matrix.

Finding the inverse matrix of a 2x2 matrix is relatively easy. All we had to do was swap 2 elements and put negative signs in front of 2 elements and then divide each element by the determinant.

Finding the inverse matrix of a 3x3 matrix or 4x4 matrix is a lot more complex and requires more complex mathematics including elementary row operations, etc. However, you don't have to actually know the math behind it because Python does everything behind the scenes for you.

So below, I now solve for the inverse matrix of a 3x3 matrix.

Even though the mathematics to compute the inverse of a 3x3 matrix is a lot more complex, Python does the work for you.



So you see great numpy is in that it can easily calculate the inverse matrix of 3x3 matrices, 4x4 matrices, etc.

You don't have to worry about the underlying mathematics behind it.

Note that not every matrix has an inverse matrix.

Remember that in order to find the inverse matrix of a matrix, you must divide each element in the matrix by the determinant. So if the determinant happens to be 0, this creates an undefined situation, since dividing by 0 is undefined.

So a matrix such as, matrix([[8,6],[4,3]]) would not have an inverse, since it has a determinant equal to 0.

This is shown in the code below.



And this is how we can get the inverse of a matrix in Python using numpy.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...