0
0
NumPydata~5 mins

np.eye() for identity matrices in NumPy - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: np.eye() for identity matrices
O(n^2)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Filling the diagonal and zeroing other elements in an n x n matrix.
  • How many times: The operation touches each element once, so n * n times.
How Execution Grows With Input

As the matrix size n grows, the number of elements to set grows with the square of n.

Input Size (n)Approx. Operations
10100
10010,000
10001,000,000

Pattern observation: Doubling n roughly quadruples the work because the matrix has n^2 elements.

Final Time Complexity

Time Complexity: O(n2)

This means the time to create the identity matrix grows with the square of the matrix size.

Common Mistake

[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.

Interview Connect

Understanding how matrix creation scales helps you reason about performance in data science tasks, especially when working with large datasets or models.

Self-Check

"What if we used a sparse matrix format instead of np.eye()? How would the time complexity change?"