Skip to Content

Numpy – Check If Matrix is an Identity Matrix

In this tutorial, we will look at how to check if a matrix is an identity matrix in Numpy with the help of some examples.

What is an identity matrix?

An identity matrix is a square matrix with all diagonal elements as 1 and all non-diagonal elements as zero. The following image shows some identity matrices.

image showing a 2x2 and a 3x3 identity matrix

How to check if a matrix is an identity matrix in Numpy?

To check if a matrix is an identity matrix or not in Numpy, use the following steps –

  1. Create an identity matrix of the same shape as the original matrix (NxN).
  2. Check if the above-created identity matrix is equal to the original matrix or not.

Now, there are multiple ways to check if two matrices are equal or not in Numpy. We’ll focus on two important ways and when they should be used.

  • Using the numpy.allclose() function.
  • Using the numpy.array_equal() function.

Let’s now look at the use cases of the above methods, with some examples.

Example 1 – Using the numpy.allclose() function

In this method, we use the numpy.allclose() function to compare the original matrix with an identity matrix of the same size. This function returns True if the input matrix is approximately equal to the identity matrix, within a specified tolerance. This is particularly useful if you have floating point values in the numpy array that may have rounding errors.

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.allclose().

Let’s look at an example.

import numpy as np

# create a numpy array
ar = np.array([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])

# create an identity matrix of the same shape
identity = np.identity(len(ar))

# check if the matrix ar an identity matrix
is_square_matrix = len(ar.shape) == 2 and ar.shape[0]
is_identity_matrix =  is_square_matrix and np.allclose(ar, identity)
print(is_identity_matrix)

Output:

True

We get True as the output. Note that as an extra check, we’re also checking if the matrix is a 2d square matrix or not.

Let’s look at an example where the matrix is not an identity matrix.

import numpy as np

# create a numpy array
ar = np.array([
    [0, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])

# create an identity matrix of the same shape
identity = np.identity(len(ar))

# check if the matrix ar an identity matrix
is_square_matrix = len(ar.shape) == 2 and ar.shape[0]
is_identity_matrix =  is_square_matrix and np.allclose(ar, identity)
print(is_identity_matrix)

Output:

False

We get False as the output.

Example 2 – Using the numpy.array_equal() method

This method is also similar to the above method, the only difference is that we use the numpy.array_equal() method to compare the matrices for equality.

The numpy.array_equal() method returns True if the matrix values are exactly equal and False other thus be mindful of not using this method on matrices that may have rounding errors.

Let’s look at an example.

import numpy as np

# create a numpy array
ar = np.array([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])

# create an identity matrix of the same shape
identity = np.identity(len(ar))

# check if the matrix ar an identity matrix
is_square_matrix = len(ar.shape) == 2 and ar.shape[0]
is_identity_matrix =  is_square_matrix and np.array_equal(ar, identity)
print(is_identity_matrix)

Output:

True

We get the same result as above.

Let’s look at an example where the matrix is not an identity matrix.

import numpy as np

# create a numpy array
ar = np.array([
    [0, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])

# create an identity matrix of the same shape
identity = np.identity(len(ar))

# check if the matrix ar an identity matrix
is_square_matrix = len(ar.shape) == 2 and ar.shape[0]
is_identity_matrix =  is_square_matrix and np.array_equal(ar, identity)
print(is_identity_matrix)

Output:

False

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. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.