In this tutorial, we will look at how to check if a numpy matrix (a 2d numpy array) is a symmetric matrix or not with the help of some examples.
What is a symmetric matrix?
A matrix is said to be symmetric if it is equal to its transpose. That is, the matrix and its transpose are the same. The following image shows a symmetric matrix.
You can see that if you take the transpose of the above matrix, you’ll get the same matrix as above.
How to check if a matrix is symmetric in Numpy?
To check if a matrix is symmetric, compare the matrix to its transpose, you can use the .T
property or the numpy.transpose()
function to get the transpose of the original matrix. You can use the numpy.array_equal()
method to compare the two matrices for equality.
import numpy as np # check if the matrix ar is symmetric np.array_equal(ar, ar.T)
In the above syntax, we’re basically checking if matric, ar
and its transpose, ar.T
are equal or not.
There are other methods as well –
- You can use the
numpy.allclose()
method to compare the two matrices for equality. Note that thenumpy.allclose()
function uses a tolerance parameter 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 thertol
(relative tolerance) oratol
(absolute tolerance) parameters ofnumpy.allclose()
.
Let’s now look at some examples of using the above syntax –
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.
Example 1 – Using the .T
attribute and numpy.array_equal()
Let’s create a square matrix and check if it is symmetric. For this, we’ll take the following steps –
- Get the transpose of the matrix using
.T
attribute. - Compare the original matrix to its transpose for equality using the
numpy.array_equal()
method.
import numpy as np # create a symmetric matrix ar = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]]) # check if the matrix ar is symmetric print(np.array_equal(ar, ar.T))
Output:
True
Here, we create a 3×3 symmetric matrix and checked if it’s symmetric or not. We get True
as the output indicating the array ar
is a symmetric matrix.
Let’s look at an example where the matrix is not a symmetric matrix.
import numpy as np # create a matrix ar = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # check if the matrix ar is symmetric print(np.array_equal(ar, ar.T))
Output:
False
We get False
as the output which indicates that the matrix ar
is not symmetric.
Example 2 – Using .T
attribute and numpy.allclose()
This method is similar to the above method, the only difference is that instead of numpy.array_equal()
function, we use the numpy.allclose()
function to compare the arrays. The following are the steps –
- Get the transpose of the matrix using the
.T
attribute. - Compare the original matrix to its transpose for equality using the
numpy.allclose()
method.
In this method, you can define how close two values need to be to be considered equal using the tolerance parameter.
Let’s take the same examples as above.
import numpy as np # create a symmetric matrix ar = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]]) # check if the matrix ar is symmetric print(np.allclose(ar, ar.T))
Output:
True
import numpy as np # create a matrix ar = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # check if the matrix ar is symmetric print(np.allclose(ar, ar.T))
Output:
False
We get the same results as above.
Example 3 – Iterate through the matrix
Alternatively, you can iterate through the entire matrix and check whether each element satisfies the symmetric matrix property, ar[i][j] == ar[j][i]
.
import numpy as np # create a symmetric matrix ar = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]]) # check if matrix is symmetric def is_matrix_symmetric(a): for i in range(len(a)): for j in range(len(a[i])): if a[i][j] == a[j][i]: continue else: return False return True # use the above function is_matrix_symmetric(ar)
Output:
True
You might also be interested in –
- How to check if a matrix is a square matrix in Numpy?
- How to check if a matrix is a diagonal matrix in Numpy?
- Numpy – Check if Matrix is a Lower Triangular Matrix
- Numpy – Check if Matrix is an Upper Triangular Matrix
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.