Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Indexing and Slicing
What will be the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
rows = [0, 2]
cols = [1, 0]
print(arr[rows, cols])
A[1 6]
B[2 6]
C[2 5]
D[3 5]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the array and indices

    arr is a 3x2 array. rows = [0, 2], cols = [1, 0].
  2. Step 2: Select elements at (0,1) and (2,0)

    arr[0,1] = 2 and arr[2,0] = 5, so output is [2 5].
  3. Final Answer:

    [2 5] -> Option C
  4. Quick Check:

    arr[[0,2],[1,0]] = [2 5] [OK]
Quick Trick: Pair row and column indices to select elements [OK]
Common Mistakes:
  • Mixing row and column indices
  • Assuming output is a submatrix
  • Using slicing instead of fancy indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes