Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Creating Arrays
What is the output of the following code?
import numpy as np
print(np.eye(3, k=1))
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.]]
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes