0
0
NumPydata~10 mins

Multi-dimensional fancy indexing in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multi-dimensional fancy indexing
Create 2D array
Define index arrays for rows and columns
Use fancy indexing: array[rows, cols
Extract elements at specified positions
Result: 1D array of selected elements
We start with a 2D array, then pick specific rows and columns using arrays of indices, and finally extract elements at those positions into a new array.
Execution Sample
NumPy
import numpy as np
arr = np.array([[10,20,30],[40,50,60],[70,80,90]])
rows = np.array([0,1,2])
cols = np.array([2,1,0])
result = arr[rows, cols]
print(result)
This code selects elements from a 2D array at positions (0,2), (1,1), and (2,0) using fancy indexing.
Execution Table
StepActionrows arraycols arrayIndexed ElementsResult
1Create 2D array---[[10 20 30] [40 50 60] [70 80 90]]
2Define rows indices[0,1,2]---
3Define cols indices-[2,1,0]--
4Apply fancy indexing arr[rows, cols][0,1,2][2,1,0][arr[0,2], arr[1,1], arr[2,0]][30 50 70]
5Print result---[30 50 70]
6End---Execution complete
💡 All specified indices processed, fancy indexing returns selected elements as 1D array.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
arr-[[10 20 30] [40 50 60] [70 80 90]][[10 20 30] [40 50 60] [70 80 90]][[10 20 30] [40 50 60] [70 80 90]][[10 20 30] [40 50 60] [70 80 90]]
rows-[0,1,2][0,1,2][0,1,2][0,1,2]
cols--[2,1,0][2,1,0][2,1,0]
result----[30 50 70]
Key Moments - 3 Insights
Why does the result have length 3 instead of being a 2D array?
Because fancy indexing with arrays of indices selects elements pairwise from each (row, col) index, returning a 1D array of those elements as shown in execution_table step 4.
What happens if rows and cols arrays have different lengths?
Numpy raises an error because fancy indexing requires the row and column index arrays to have the same shape to pair elements correctly, as implied in step 4.
Why do we use arrays for rows and cols instead of lists?
Numpy fancy indexing works best with numpy arrays for efficient element-wise pairing and broadcasting, as shown in variable_tracker steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what element does arr[1,1] correspond to?
A20
B50
C70
D30
💡 Hint
Check the 'Indexed Elements' column at step 4 in execution_table.
At which step do we get the final result array [30, 50, 70]?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look at the 'Result' column in execution_table rows for step 4.
If we change cols to [0,0,0], what will be the result array?
A[10, 40, 70]
B[30, 50, 70]
C[10, 50, 90]
D[30, 40, 70]
💡 Hint
Refer to variable_tracker for cols array and how indexing pairs rows and cols.
Concept Snapshot
Multi-dimensional fancy indexing lets you pick elements from a numpy array using arrays of row and column indices.
Syntax: arr[rows_array, cols_array]
Rows and cols arrays must be same shape.
Result is a 1D array of elements at paired indices.
Useful for selecting scattered elements efficiently.
Full Transcript
We start with a 2D numpy array and two arrays of indices for rows and columns. Using fancy indexing, we select elements at positions defined by pairing each row index with the corresponding column index. This returns a 1D array of those elements. The process requires the row and column index arrays to have the same shape. This method is useful to extract specific elements scattered across the array efficiently.