Bird
0
0

Given a 2D NumPy array arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), how can you use fancy indexing to select elements at positions (0,2), (1,0), and (2,1) and create a new 1D array with these values?

hard📝 Application Q15 of 15
NumPy - Indexing and Slicing
Given a 2D NumPy array arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), how can you use fancy indexing to select elements at positions (0,2), (1,0), and (2,1) and create a new 1D array with these values?
Aarr[[0,2,1], [2,1,0]]
Barr[[0,1,2], [2,0,1]]
Carr[[2,1,0], [0,1,2]]
Darr[[0,1,2]][[2,0,1]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand 2D fancy indexing

    To select elements at (row, col) positions, use two index arrays: one for rows, one for columns.
  2. Step 2: Match given positions to index arrays

    Positions are (0,2), (1,0), (2,1), so rows = [0,1,2], cols = [2,0,1].
  3. Step 3: Use fancy indexing syntax

    Use arr[[0,1,2], [2,0,1]] to get elements [3,4,8].
  4. Final Answer:

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

    Rows and cols arrays match positions [OK]
Quick Trick: Use two index arrays: rows and columns [OK]
Common Mistakes:
  • Mixing row and column indices order
  • Using chained indexing instead of fancy indexing
  • Using wrong index arrays for positions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes