Bird
0
0

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📝 Application Q15 of 15
NumPy - Creating Arrays
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?
A<code>np.eye(5, k=-1)</code> resulting in 1s at positions (1,0), (2,1), (3,2), (4,3)
B<code>np.eye(5, k=1)</code> resulting in 1s at positions (0,1), (1,2), (2,3), (3,4)
C<code>np.eye(5)</code> resulting in 1s on the main diagonal
D<code>np.eye(5, k=-2)</code> resulting in 1s two diagonals below the main
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes