Challenge - 5 Problems
Advanced Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of basic vs advanced indexing
What is the output of the following code comparing basic and advanced indexing in NumPy?
NumPy
import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) result_basic = arr[1, 2] result_advanced = arr[[1, 2], [2, 0]] print(result_basic) print(result_advanced)
Attempts:
2 left
💡 Hint
Remember that basic indexing selects a single element, while advanced indexing can select multiple elements using arrays of indices.
✗ Incorrect
Basic indexing arr[1, 2] selects the element at row 1, column 2, which is 60. Advanced indexing arr[[1, 2], [2, 0]] selects elements at positions (1,2) and (2,0), which are 60 and 70 respectively.
❓ data_output
intermediate1:30remaining
Shape of array after advanced indexing
What is the shape of the array produced by this advanced indexing operation?
NumPy
import numpy as np arr = np.arange(24).reshape(4,6) indexed = arr[[0, 2], [1, 4]] print(indexed.shape)
Attempts:
2 left
💡 Hint
Advanced indexing with two 1D arrays returns a 1D array with length equal to the arrays' length.
✗ Incorrect
The two index arrays [0, 2] and [1, 4] select two elements, so the result is a 1D array of length 2, shape (2,).
🔧 Debug
advanced2:00remaining
Identify the error in advanced indexing
What error does this code raise when using advanced indexing incorrectly?
NumPy
import numpy as np arr = np.array([[1,2,3],[4,5,6]]) indices = [0, 1, 2] result = arr[indices, 1]
Attempts:
2 left
💡 Hint
Check the size of the first dimension of the array and the indices used.
✗ Incorrect
The array has shape (2,3), so index 2 is out of bounds for axis 0 (rows). Using indices = [0,1,2] tries to access row 2 which does not exist, causing IndexError.
🚀 Application
advanced2:30remaining
Using advanced indexing to modify array elements
Given the array below, which option correctly sets the elements at positions (0,1), (1,2), and (2,0) to 99 using advanced indexing?
NumPy
import numpy as np arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
Attempts:
2 left
💡 Hint
Use advanced indexing with two lists of indices to assign values to specific positions.
✗ Incorrect
Option D uses advanced indexing correctly to assign 99 to the specified positions. Option D uses comparison instead of assignment. Option D is invalid syntax. Option D adds 99 instead of setting.
🧠 Conceptual
expert3:00remaining
Why advanced indexing matters for performance
Why is advanced indexing important when working with large NumPy arrays?
Attempts:
2 left
💡 Hint
Think about how advanced indexing helps when you want specific elements scattered in the array.
✗ Incorrect
Advanced indexing lets you pick multiple arbitrary elements efficiently, often returning a new array with just those elements, which is faster and uses less memory than copying or looping manually.