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.
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 –
- Create an identity matrix of the same shape as the original matrix (NxN).
- 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
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 –
- Numpy – Check If Two Matrices are Equal
- Numpy – Check If a Matrix is Orthogonal
- Numpy – Check If a Matrix is Invertible
- How to check if a matrix is symmetric in Numpy?
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.