How to Create an Identity Matrix in NumPy Easily
You can create an identity matrix in NumPy using the
numpy.eye() function, which returns a 2-D array with ones on the diagonal and zeros elsewhere. Specify the size with the N parameter to get an N x N identity matrix.Syntax
The basic syntax to create an identity matrix in NumPy is:
numpy.eye(N, M=None, k=0, dtype=float, order='C')
Where:
N: Number of rows (and columns ifMis not given).M: Number of columns (optional, defaults toN).k: Index of the diagonal (0 for main diagonal, positive for above, negative for below).dtype: Data type of the output array.order: Memory layout ('C' for row-major, 'F' for column-major).
python
import numpy as np # Create a 3x3 identity matrix identity_matrix = np.eye(3) print(identity_matrix)
Output
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Example
This example shows how to create a 4x4 identity matrix and a 3x5 matrix with ones on the main diagonal using numpy.eye().
python
import numpy as np # 4x4 identity matrix identity_4x4 = np.eye(4) print('4x4 Identity Matrix:') print(identity_4x4) # 3x5 matrix with ones on main diagonal identity_3x5 = np.eye(3, 5) print('\n3x5 Matrix with ones on main diagonal:') print(identity_3x5)
Output
4x4 Identity Matrix:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
3x5 Matrix with ones on main diagonal:
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]]
Common Pitfalls
Common mistakes when creating identity matrices include:
- Confusing
numpy.eye()withnumpy.identity(). Theidentity()function only creates square matrices, whileeye()can create rectangular matrices. - Forgetting that the default diagonal is the main diagonal (
k=0), so changingkshifts the diagonal of ones. - Not specifying
dtypewhen you need a specific data type, which defaults tofloat.
Example of a wrong and right way:
python
import numpy as np # Wrong: expecting identity() to create non-square matrix (this will error) try: np.identity(3, 5) except TypeError as e: print('Error:', e) # Right: use eye() for non-square matrix rect_matrix = np.eye(3, 5) print('\nCorrect rectangular identity matrix with eye():') print(rect_matrix)
Output
Error: identity() takes exactly one argument (2 given)
Correct rectangular identity matrix with eye():
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]]
Quick Reference
Summary tips for creating identity matrices in NumPy:
- Use
numpy.eye(N)for anN x Nidentity matrix. - Use
numpy.eye(N, M)for rectangular matrices with ones on the main diagonal. - Adjust
kto move the diagonal of ones up or down. - Specify
dtypeif you need integers or other types instead of floats.
Key Takeaways
Use numpy.eye() to create identity matrices with flexible sizes and shapes.
The parameter k controls which diagonal has the ones; 0 means the main diagonal.
numpy.identity() only creates square identity matrices and takes one argument.
Specify dtype in numpy.eye() to control the data type of the matrix elements.
For rectangular identity-like matrices, always prefer numpy.eye() over numpy.identity().