Bird
Raised Fist0
NumPydata~5 mins

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

Choose your learning style10 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?"

Practice

(1/5)
1. What does the function np.eye(4) create in NumPy?
easy
A. A 4x4 identity matrix with 1s on the main diagonal
B. A 4x4 matrix filled with zeros
C. A 4x4 matrix filled with ones
D. A 4x4 diagonal matrix with 1s on the first upper diagonal

Solution

  1. Step 1: Understand np.eye() default behavior

    By default, np.eye(n) creates an n x n identity matrix with 1s on the main diagonal and 0s elsewhere.
  2. Step 2: Apply np.eye(4)

    Calling np.eye(4) creates a 4x4 matrix with 1s on the main diagonal and 0s elsewhere.
  3. Final Answer:

    A 4x4 identity matrix with 1s on the main diagonal -> Option A
  4. Quick Check:

    np.eye(4) = 4x4 identity matrix [OK]
Hint: np.eye(n) makes nxn identity matrix by default [OK]
Common Mistakes:
  • Confusing np.eye() with np.ones() or np.zeros()
  • Thinking np.eye() fills all elements with 1
  • Assuming 1s are on an off-diagonal by default
2. Which of the following is the correct syntax to create a 3x5 identity matrix with 1s on the main diagonal using np.eye()?
easy
A. np.eye(5, 3)
B. np.eye(3, 5)
C. np.eye(3)
D. np.eye(5)

Solution

  1. Step 1: Understand np.eye() parameters

    The first parameter is the number of rows, the second is columns. So np.eye(3, 5) creates a 3x5 matrix.
  2. Step 2: Check the options

    np.eye(3, 5) correctly uses np.eye(3, 5) to create a 3x5 matrix with 1s on the main diagonal.
  3. Final Answer:

    np.eye(3, 5) -> Option B
  4. Quick Check:

    np.eye(rows, cols) = correct syntax [OK]
Hint: First arg = rows, second = columns in np.eye() [OK]
Common Mistakes:
  • Swapping rows and columns parameters
  • Using only one parameter for non-square matrix
  • Confusing np.eye() with np.identity()
3. What is the output of the following code?
import numpy as np
print(np.eye(3, k=1))
medium
A. [[0. 0. 1.] [0. 1. 0.] [1. 0. 0.]]
B. [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
C. [[0. 0. 0.] [1. 0. 0.] [0. 1. 0.]]
D. [[0. 1. 0.] [0. 0. 1.] [0. 0. 0.]]

Solution

  1. Step 1: Understand the k parameter in np.eye()

    The parameter k=1 shifts the diagonal of 1s one above the main diagonal.
  2. Step 2: Visualize the 3x3 matrix with 1s on the first upper diagonal

    The 1s appear at positions (0,1) and (1,2), zeros elsewhere.
  3. Final Answer:

    [[0. 1. 0.] [0. 0. 1.] [0. 0. 0.]] -> Option D
  4. Quick Check:

    k=1 shifts diagonal up by 1 [OK]
Hint: k shifts diagonal: 0=main, 1=above, -1=below [OK]
Common Mistakes:
  • Ignoring the k parameter effect
  • Assuming k=1 means main diagonal
  • Confusing rows and columns in output
4. The code below throws an error. What is the mistake?
import numpy as np
matrix = np.eye(4, 3, k=4)
print(matrix)
medium
A. np.eye() does not accept three arguments
B. The matrix must be square for np.eye()
C. The value of k is too large for the matrix shape
D. The print statement is incorrect

Solution

  1. Step 1: Check the meaning of k relative to matrix size

    The parameter k=4 means the diagonal of 1s is shifted 4 above the main diagonal.
  2. Step 2: Compare k with matrix dimensions

    For a 4x3 matrix, the maximum upper diagonal shift is 2 (columns - 1). k=4 is invalid and causes an error.
  3. Final Answer:

    The value of k is too large for the matrix shape -> Option C
  4. Quick Check:

    k must be within valid diagonal range [OK]
Hint: k must fit within matrix diagonal limits [OK]
Common Mistakes:
  • Assuming any k value works
  • Thinking np.eye() only creates square matrices
  • Blaming print instead of parameter error
5. You want to create a 5x5 matrix with 1s on the diagonal just below the main diagonal using np.eye(). Which code achieves this and what is the resulting matrix?
hard
A. np.eye(5, k=-1) resulting in 1s at positions (1,0), (2,1), (3,2), (4,3)
B. np.eye(5, k=1) resulting in 1s at positions (0,1), (1,2), (2,3), (3,4)
C. np.eye(5) resulting in 1s on the main diagonal
D. np.eye(5, k=-2) resulting in 1s two diagonals below the main

Solution

  1. Step 1: Understand k parameter for diagonal shift

    Negative k shifts the diagonal of 1s below the main diagonal. k=-1 means one diagonal below.
  2. Step 2: Apply np.eye(5, k=-1)

    This creates a 5x5 matrix with 1s at positions (1,0), (2,1), (3,2), (4,3) and zeros elsewhere.
  3. Final Answer:

    np.eye(5, k=-1) with 1s just below main diagonal -> Option A
  4. Quick Check:

    k=-1 shifts diagonal down by 1 [OK]
Hint: Use negative k for diagonals below main [OK]
Common Mistakes:
  • Using positive k for below main diagonal
  • Confusing positions of 1s in matrix
  • Assuming np.eye() only creates main diagonal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard