Challenge - 5 Problems
Fancy Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of fancy indexing with repeated indices
What is the output of the following code snippet using NumPy fancy indexing?
NumPy
import numpy as np arr = np.array([10, 20, 30, 40, 50]) result = arr[[1, 2, 2, 4]] print(result)
Attempts:
2 left
💡 Hint
Remember that fancy indexing returns elements at the specified indices, including duplicates.
✗ Incorrect
The array is indexed at positions 1, 2, 2, and 4. Position 1 is 20, position 2 is 30, repeated twice, and position 4 is 50.
❓ data_output
intermediate2:00remaining
Shape of result after 2D fancy indexing
Given the code below, what is the shape of the resulting array?
NumPy
import numpy as np arr = np.arange(16).reshape(4,4) indices = np.array([[0, 1], [2, 3]]) result = arr[indices] print(result.shape)
Attempts:
2 left
💡 Hint
Fancy indexing with a 2D array returns an array with shape equal to the indices shape plus the remaining dimensions.
✗ Incorrect
The indices array has shape (2,2). Each index selects a row of length 4 from arr, so the result shape is (2,2,4).
🔧 Debug
advanced2:00remaining
Identify the error in fancy indexing with mixed types
What error will the following code raise?
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5]) indices = [1, '2', 3] result = arr[indices] print(result)
Attempts:
2 left
💡 Hint
Check the types inside the indices list used for indexing.
✗ Incorrect
The list contains a string '2' which is not a valid integer index, causing a TypeError when used for fancy indexing.
🚀 Application
advanced2:00remaining
Extracting specific elements using fancy indexing
You have a 2D NumPy array representing scores of 5 students in 4 subjects:
scores = np.array([[80, 90, 70, 60],
[85, 95, 75, 65],
[78, 88, 68, 58],
[82, 92, 72, 62],
[79, 89, 69, 59]])
You want to extract the scores of students 0, 2, and 4 in subjects 1 and 3 using fancy indexing. Which code snippet produces the correct result?NumPy
import numpy as np scores = np.array([[80, 90, 70, 60], [85, 95, 75, 65], [78, 88, 68, 58], [82, 92, 72, 62], [79, 89, 69, 59]])
Attempts:
2 left
💡 Hint
Remember that fancy indexing with two arrays selects elements pairwise, but you want a submatrix.
✗ Incorrect
Option D first selects the rows 0, 2, and 4, then selects columns 1 and 3, producing a 3x2 array of the desired scores.
🧠 Conceptual
expert2:00remaining
Understanding broadcasting in fancy indexing
Consider the following code snippet:
import numpy as np arr = np.arange(12).reshape(3,4) rows = np.array([0, 1, 2]) cols = np.array([[0, 1], [2, 3]]) result = arr[rows[:, None], cols] print(result)What is the shape of
result and why?Attempts:
2 left
💡 Hint
Think about how broadcasting works between rows[:, None] and cols arrays during indexing.
✗ Incorrect
rows[:, None] has shape (3,1) and cols has shape (2,2). They broadcast to (3,2,2), so result shape is (3,2,2).