Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Creating Arrays
What is the output of the following code?
import numpy as np
print(np.eye(3, 5, k=2))
A[[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.]]
B[[0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]]
C[[0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.]]
D[[0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.] [0. 0. 0. 0. 0.]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameters

    np.eye(3, 5, k=2) creates a 3x5 matrix with ones on the diagonal shifted 2 above the main diagonal.
  2. Step 2: Position of ones

    On each row i, the one is at column i + 2.
  3. Step 3: Construct matrix

    Row 0: one at col 2, Row 1: col 3, Row 2: col 4.
  4. Final Answer:

    [[0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]] -> Option B
  5. Quick Check:

    Check diagonal offset and matrix shape [OK]
Quick Trick: k shifts diagonal; positive k moves ones right [OK]
Common Mistakes:
  • Ignoring k parameter shifts
  • Assuming square matrix always
  • Misplacing ones on wrong diagonal

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes