0
0
PythonHow-ToBeginner · 3 min read

How to Create Identity Matrix in Python Easily

You can create an identity matrix in Python using numpy.eye(n) where n is the size of the matrix. Alternatively, use a nested list comprehension to build it manually with 1s on the diagonal and 0s elsewhere.
📐

Syntax

The most common way to create an identity matrix is using the numpy.eye(n) function.

  • n: size of the identity matrix (number of rows and columns)
  • The function returns a 2D array with 1s on the diagonal and 0s elsewhere.

Alternatively, you can use a nested list comprehension:

[[1 if i == j else 0 for j in range(n)] for i in range(n)]

This creates a list of lists with 1s on the diagonal and 0s elsewhere.

python
import numpy as np

# Using numpy to create identity matrix
identity_matrix = np.eye(4)
print(identity_matrix)

# Using list comprehension to create identity matrix
n = 4
identity_list = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
print(identity_list)
Output
[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
💻

Example

This example shows how to create a 3x3 identity matrix using numpy and pure Python. It prints both results so you can see the difference in output format.

python
import numpy as np

# Create 3x3 identity matrix with numpy
identity_np = np.eye(3)
print("Numpy identity matrix:")
print(identity_np)

# Create 3x3 identity matrix with list comprehension
n = 3
identity_list = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
print("List comprehension identity matrix:")
print(identity_list)
Output
Numpy identity matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] List comprehension identity matrix: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
⚠️

Common Pitfalls

One common mistake is confusing numpy.eye(n) with numpy.identity(n). Both create identity matrices, but eye allows more control like offsetting the diagonal.

Another pitfall is forgetting that list comprehension creates a list of lists, not a numpy array, so some matrix operations won't work directly.

python
import numpy as np

# Wrong: expecting numpy array but using list of lists
n = 3
identity_wrong = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
try:
    print(identity_wrong + identity_wrong)  # This will concatenate lists, not add matrices
except Exception as e:
    print("Error:", e)

# Right: convert list to numpy array for matrix operations
identity_correct = np.array(identity_wrong)
print(identity_correct + identity_correct)  # This adds matrices element-wise
Output
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1]] [[2 0 0] [0 2 0] [0 0 2]]
📊

Quick Reference

  • numpy.eye(n): Creates an n x n identity matrix as a numpy array.
  • numpy.identity(n): Also creates an identity matrix, but less flexible.
  • List comprehension: Creates a list of lists representing the identity matrix.
  • Use numpy arrays for matrix math, lists for simple data storage.

Key Takeaways

Use numpy.eye(n) for a simple and flexible way to create identity matrices.
List comprehension can create identity matrices without extra libraries but returns lists.
Convert lists to numpy arrays for matrix operations to avoid errors.
numpy.identity(n) is similar but less flexible than numpy.eye(n).
Remember identity matrices have 1s on the diagonal and 0s elsewhere.