While the general rules of matrix operations apply to all matrices that meet the right dimensional requirements, certain matrices have special structures and properties that make them particularly useful. Recognizing these structures can simplify calculations and provides a better understanding of the transformations or data they represent. Some of the most common special matrices encountered in machine learning will be presented.
Before we discuss other types, we must first define a square matrix. This is simply a matrix that has the same number of rows and columns. A matrix with dimensions n×n is a square matrix.
A=438951276The matrix A above is a 3×3 square matrix. Many of the special matrices we will look at, including identity, diagonal, and symmetric matrices, are required to be square.
The identity matrix, denoted as I or In, is the matrix equivalent of the number 1. In regular arithmetic, multiplying any number by 1 leaves it unchanged. Similarly, multiplying any matrix by the identity matrix leaves the original matrix unchanged.
An identity matrix is a square matrix with 1s on the main diagonal (the elements from the top-left to the bottom-right) and 0s everywhere else.
Here are the identity matrices of size 2×2 and 3×3:
I2=[1001]I3=100010001The primary property of the identity matrix is that for any matrix A, the following is true:
A⋅I=I⋅A=AThis property is fundamental for solving systems of linear equations, which we will cover in the next chapter.
You can create an identity matrix easily using NumPy's eye() function.
import numpy as np
# Create a 3x3 identity matrix
I = np.eye(3)
print(I)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
A diagonal matrix is a square matrix where all elements not on the main diagonal are zero. The elements on the main diagonal can be any value, including zero. The identity matrix is a specific type of diagonal matrix where all diagonal elements are 1.
Here is an example of a 3×3 diagonal matrix:
D=7000−20005Diagonal matrices are computationally efficient. Multiplying a matrix by a diagonal matrix has the effect of scaling the rows or columns of that matrix, which is an important operation in linear transformations. For example, they can be used to scale individual features in a dataset.
NumPy's diag() function can be used to create a diagonal matrix from a list or array of numbers.
import numpy as np
# Create a diagonal matrix from a list of values
D = np.diag([7, -2, 5])
print(D)
[[ 7 0 0]
[ 0 -2 0]
[ 0 0 5]]
A symmetric matrix is a square matrix that is identical to its transpose. In other words, a matrix A is symmetric if:
A=ATThis means that the element in the i-th row and j-th column is equal to the element in the j-th row and i-th column. The matrix is a mirror image of itself across the main diagonal.
Here is an example of a symmetric matrix:
S=18−3874−349Notice how the element at (1,2) is 8, and the element at (2,1) is also 8. The element at (1,3) is -3, and so is the element at (3,1).
Symmetric matrices appear frequently in machine learning. One of the most common examples is the covariance matrix, which describes the variance and covariance between different features in a dataset. Since the covariance between feature X and feature Y is the same as between Y and X, the resulting matrix is always symmetric.
You can check if a matrix is symmetric by comparing it to its transpose.
import numpy as np
S = np.array([[1, 8, -3],
[8, 7, 4],
[-3, 4, 9]])
# Check for symmetry
is_symmetric = (S == S.T).all()
print(f"Is the matrix symmetric? {is_symmetric}")
Is the matrix symmetric? True
The diagram below shows how these special matrix types relate to each other.
A diagram showing the relationship between different types of square matrices. For example, an identity matrix is a more specific version of a diagonal matrix.
Two other matrix types worth knowing are triangular matrices and the zero matrix.
These special matrices form a toolkit that simplifies many problems in linear algebra and machine learning. Recognizing them allows you to apply efficient algorithms and better understand the structure of your data.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with