check matrix is invertible in numpy

Numpy – Check If a Matrix is Invertible

In this tutorial, we will look at how to check if a numpy matrix (a 2d numpy array) is invertible or not with the help of some examples.

When is a matrix invertible?

A square matrix, for example, M is said to be invertible, if there exists a matrix N such that, MN = NM = I where I is an identity matrix. The following image shows an invertible matrix.

image showing a 2x2 invertible matrix and its inverse

Alternatively, we can say that N is equal to the inverse of the matrix M. That is, a matrix is invertible if you can take its inverse.

A square matrix that is not invertible is called a singular matrix.

How to check if a matrix is invertible in Numpy?

To check if a matrix is invertible or not in Numpy, check if it has a non-zero determinant.

If a matrix has a non-zero determinant (the determinant is not zero), we can say that the matrix is invertible. Use the numpy.linalg.det() function to compute the determinant of a matrix. The following is the syntax –

import numpy as np

# check if matrix ar is invertible
not np.isclose(np.linalg.det(ar), 0)

Let’s now look at the above method with the help of some examples. First, we will create two matrices that we will use throughout this tutorial, one invertible and the other non-invertible.

import numpy as np

# create matrices
ar1 = np.array([
    [1, 2, 3],
    [0, 4, 5],
    [0, 0, 6]
])

ar2 = np.array([
    [2, 4, 6],
    [2, 0, 2],
    [6, 8, 14]
])

Here, the matrix ar1 is invertible and the matrix ar2 is non-invertible.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Example – Check if the determinant is non-zero

In this method, we calculate the determinant of the matrix using the numpy.linalg.det() function and check whether it is non-zero or not. If the determinant is non-zero, we say the matrix is invertible.

Let’s apply this method to the matrices created above.

# check if ar1 is invertible
print(np.linalg.det(ar1) != 0)

# check if ar2 is invertible
print(np.linalg.det(ar2) != 0)

Output:

True
True

We get True for both matrices, which is not the correct answer. The matrix ar1 is invertible, so the first True is okay, but the matrix ar2 is singular and this method should give False but we get True.

Why is this happening? Let’s print out the determinant for ar2.

np.linalg.det(ar2)

Output:

7.105427357600985e-15

We get a very small value which is not exactly zero.

To work around this, you can use the numpy.isclose() function which lets you define a tolerance to determine how close two values need to be to be considered equal. By default, the tolerance is set to 1e-05, which means that two values are considered equal if they are within 0.00001 of each other. You can adjust this tolerance by passing a different value to the rtol (relative tolerance) or atol (absolute tolerance) parameters of numpy.isclose().

Using the numpy.isclose() function to compare the results from numpy.linalg.det() to zero helps resolve the above issue.

# check if ar1 is invertible
print(not np.isclose(np.linalg.det(ar1), 0))

# check if ar2 is invertible
print(not np.isclose(np.linalg.det(ar2), 0))

Output:

True
False

We now get the correct answer. In the above example, we first check whether the determinant is zero (very close to zero) or not, if it isn’t, we say that the matric is invertible.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top