np.eye() for identity matrices in NumPy - Time & Space Complexity
We want to understand how the time to create an identity matrix with np.eye() changes as the matrix size grows.
How does the work needed grow when the matrix gets bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 5
identity_matrix = np.eye(n)
print(identity_matrix)
This code creates an n x n identity matrix, where the diagonal elements are 1 and others are 0.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filling the diagonal and zeroing other elements in an
n x nmatrix. - How many times: The operation touches each element once, so
n * ntimes.
As the matrix size n grows, the number of elements to set grows with the square of n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 |
| 100 | 10,000 |
| 1000 | 1,000,000 |
Pattern observation: Doubling n roughly quadruples the work because the matrix has n^2 elements.
Time Complexity: O(n2)
This means the time to create the identity matrix grows with the square of the matrix size.
[X] Wrong: "Creating an identity matrix takes time proportional to n because only the diagonal is set."
[OK] Correct: Even though only the diagonal is 1, the function must create and initialize all n^2 elements, so the work depends on the full matrix size.
Understanding how matrix creation scales helps you reason about performance in data science tasks, especially when working with large datasets or models.
"What if we used a sparse matrix format instead of np.eye()? How would the time complexity change?"