Advanced indexing helps you pick specific parts of data quickly and easily. It makes working with big data faster and clearer.
Why advanced indexing matters in NumPy
array[indices] array[[row_indices], [col_indices]]
Indices can be lists, arrays, or boolean masks.
Advanced indexing returns a copy, not a view, so changes do not affect the original array unless assigned back.
import numpy as np arr = np.array([10, 20, 30, 40, 50]) print(arr[[1, 3, 4]])
arr2d = np.array([[1, 2], [3, 4], [5, 6]]) print(arr2d[[0, 2], [1, 0]])
mask = np.array([True, False, True, False, True]) print(arr[mask])
This code picks specific elements from the matrix using advanced indexing. Then it changes those elements to zero all at once.
import numpy as np # Create a 2D array matrix = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) # Use advanced indexing to pick elements at (0,2), (1,1), and (2,0) rows = [0, 1, 2] cols = [2, 1, 0] selected = matrix[rows, cols] print("Selected elements:", selected) # Change these selected elements to 0 matrix[rows, cols] = 0 print("Matrix after changes:") print(matrix)
Advanced indexing can combine multiple index arrays to pick exact elements.
It is faster and cleaner than looping over elements one by one.
Remember that the result is a copy, so modifying it won't change the original array unless you assign back.
Advanced indexing lets you pick and change many specific elements easily.
It works with lists, arrays, or boolean masks as indexes.
It helps write clear and fast code for data selection and modification.
Practice
Solution
Step 1: Understand what advanced indexing does
Advanced indexing lets you pick many specific elements from an array using lists or arrays of indices.Step 2: Compare options with this understanding
Only It allows selecting multiple specific elements easily and efficiently. describes this benefit correctly; others describe unrelated features.Final Answer:
It allows selecting multiple specific elements easily and efficiently. -> Option AQuick Check:
Advanced indexing = easy multi-element selection [OK]
- Thinking it sorts or changes data types
- Confusing with basic slicing
- Assuming it converts arrays to lists
arr?Solution
Step 1: Recall advanced indexing syntax
Advanced indexing uses square brackets with a list or array of indices inside, like arr[[0, 2, 4]].Step 2: Check each option
arr[[0, 2, 4]] uses correct syntax. Options A, C, and D use invalid syntax for indexing.Final Answer:
arr[[0, 2, 4]] -> Option BQuick Check:
Use double brackets with list for advanced indexing [OK]
- Using parentheses instead of brackets
- Using slicing syntax incorrectly
- Using curly braces which are invalid
import numpy as np arr = np.array([10, 20, 30, 40, 50]) indices = [1, 3] print(arr[indices])
Solution
Step 1: Understand the array and indices
The array is [10, 20, 30, 40, 50]. The indices list is [1, 3].Step 2: Apply advanced indexing
arr[indices] selects elements at positions 1 and 3, which are 20 and 40.Final Answer:
[20 40] -> Option CQuick Check:
arr[[1,3]] = [20 40] [OK]
- Confusing indices with values
- Expecting a slice output
- Printing indices instead of array elements
import numpy as np arr = np.array([1, 2, 3, 4, 5]) indices = [0, 2, 5] print(arr[indices])
Solution
Step 1: Check the array size and indices
The array has 5 elements with indices 0 to 4. The indices list includes 5, which is invalid.Step 2: Understand the error cause
Accessing arr[5] causes an IndexError because index 5 is out of bounds.Final Answer:
Index 5 is out of bounds for the array. -> Option AQuick Check:
Index must be within array size [OK]
- Thinking list type causes error
- Assuming advanced indexing needs 2D arrays
- Ignoring out-of-range indices
arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), how can you use advanced indexing to select elements 2, 6, and 7 from the array?Solution
Step 1: Identify positions of elements 2, 6, and 7
Element 2 is at (0,1), 6 at (1,2), and 7 at (2,0).Step 2: Use advanced indexing with row and column arrays
Using arr[[0,1,2], [1,2,0]] selects these elements by pairing rows and columns.Final Answer:
arr[[0,1,2], [1,2,0]] -> Option DQuick Check:
Pair row and column indices to select elements [OK]
- Using invalid indices out of range
- Trying to slice instead of advanced indexing
- Misaligning row and column index arrays
Ready to prove it?
Pass the test and this pattern moves unknown
