Specific types of matrices, which are rectangular grids of numbers with rows and columns, possess unique structures and properties. Recognizing these special forms is helpful because they appear often in calculations and theoretical discussions within linear algebra and machine learning.
The most straightforward special type is the square matrix. As the name suggests, a square matrix is one that has the same number of rows as columns. If a matrix has rows and columns (an matrix), it is square if .
For example, here are a couple of square matrices:
A square matrix:
A square matrix:
Square matrices are significant because many important matrix operations and concepts, such as matrix inversion and determinants (which we'll touch upon later), are primarily defined for them.
In NumPy, a square matrix is simply a 2D array where the first dimension (number of rows) equals the second dimension (number of columns). You can check this using the .shape attribute.
import numpy as np
B = np.array([[1, 2],
[3, 4]])
print(f"Matrix B:\n{B}")
print(f"Shape of B: {B.shape}") # Output: Shape of B: (2, 2)
print(f"Is B square? {B.shape[0] == B.shape[1]}") # Output: Is B square? True
C = np.array([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
print(f"\nMatrix C:\n{C}")
print(f"Shape of C: {C.shape}") # Output: Shape of C: (3, 3)
print(f"Is C square? {C.shape[0] == C.shape[1]}") # Output: Is C square? True
# A non-square matrix example
D = np.array([[1, 2, 3],
[4, 5, 6]])
print(f"\nMatrix D:\n{D}")
print(f"Shape of D: {D.shape}") # Output: Shape of D: (2, 3)
print(f"Is D square? {D.shape[0] == D.shape[1]}") # Output: Is D square? False
The identity matrix, usually denoted as or sometimes (where specifies the size), is a special square matrix that plays a role similar to the number 1 in regular multiplication.
An identity matrix has 1s along its main diagonal (the diagonal from the top-left corner to the bottom-right corner) and 0s everywhere else.
Here are the and identity matrices:
The main property of the identity matrix is that when you multiply it by any other matrix (of compatible dimensions), the result is just . That is, and . This property makes it fundamental in matrix algebra, especially when discussing matrix inverses.
NumPy provides convenient functions to create identity matrices: np.identity(n) or np.eye(n). Both create an identity matrix.
import numpy as np
# Create a 3x3 identity matrix
I_3 = np.identity(3)
print(f"3x3 Identity Matrix (using np.identity):\n{I_3}")
# Create a 4x4 identity matrix using np.eye
I_4 = np.eye(4)
print(f"\n4x4 Identity Matrix (using np.eye):\n{I_4}")
The zero matrix, often denoted as , is a matrix where every single element is zero. Unlike the identity matrix, a zero matrix does not have to be square. It can have any dimensions ().
Here are a few examples:
A zero matrix:
A (square) zero matrix:
The zero matrix acts like the number 0 in standard arithmetic. Adding a zero matrix to another matrix (of the same dimensions) leaves unchanged (). Multiplying any matrix by a zero matrix (of compatible dimensions) results in a zero matrix (, ).
In NumPy, you can easily create zero matrices using the np.zeros() function, specifying the desired shape as a tuple (rows, columns).
import numpy as np
# Create a 2x3 zero matrix
O_2x3 = np.zeros((2, 3))
print(f"2x3 Zero Matrix:\n{O_2x3}")
# Create a 3x3 zero matrix
O_3x3 = np.zeros((3, 3))
print(f"\n3x3 Zero Matrix:\n{O_3x3}")
Understanding these basic matrix types. square, identity, and zero. provides a foundation for exploring more complex matrix properties and operations in the sections that follow. You'll encounter these fundamental structures frequently as you work with linear algebra for machine learning.
Was this section helpful?
identity function, useful for creating identity matrices in Python.zeros function, providing details on creating zero matrices.© 2026 ApX Machine LearningAI Ethics & Transparency•