0
0
NumPydata~20 mins

Why advanced indexing matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
60
[50 70]
B
60
[60 70]
C
[40 50]
[60 70]
D
60
[40 90]
Attempts:
2 left
💡 Hint
Remember that basic indexing selects a single element, while advanced indexing can select multiple elements using arrays of indices.
data_output
intermediate
1: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)
A(2, 2)
B(1, 2)
C(2,)
D(4,)
Attempts:
2 left
💡 Hint
Advanced indexing with two 1D arrays returns a 1D array with length equal to the arrays' length.
🔧 Debug
advanced
2: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]
AIndexError: index 2 is out of bounds for axis 0 with size 2
BValueError: shape mismatch: indexing arrays could not be broadcast together
CTypeError: list indices must be integers or slices, not list
DNo error, returns array([2, 5, 6])
Attempts:
2 left
💡 Hint
Check the size of the first dimension of the array and the indices used.
🚀 Application
advanced
2: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]])
Aarr[[0,1,2], [1,2,0]] += 99
Barr[[0,1,2], [1,2,0]] == 99
Carr[0,1,2][1,2,0] = 99
Darr[[0,1,2], [1,2,0]] = 99
Attempts:
2 left
💡 Hint
Use advanced indexing with two lists of indices to assign values to specific positions.
🧠 Conceptual
expert
3:00remaining
Why advanced indexing matters for performance
Why is advanced indexing important when working with large NumPy arrays?
AIt allows selecting multiple arbitrary elements efficiently without copying the entire array.
BIt automatically sorts the array elements during selection.
CIt converts arrays into lists for faster processing.
DIt prevents any changes to the original array by creating a deep copy always.
Attempts:
2 left
💡 Hint
Think about how advanced indexing helps when you want specific elements scattered in the array.