0
0
NumPydata~20 mins

Fancy indexing with integer arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fancy Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[10 20 30 40]
B[20 30 30 50]
C[1 2 2 4]
D[20 30 40 50]
Attempts:
2 left
💡 Hint
Remember that fancy indexing returns elements at the specified indices, including duplicates.
data_output
intermediate
2: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)
A(2, 2, 4)
B(4, 4)
C(2, 4)
D(2, 2)
Attempts:
2 left
💡 Hint
Fancy indexing with a 2D array returns an array with shape equal to the indices shape plus the remaining dimensions.
🔧 Debug
advanced
2: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)
AValueError: invalid literal for int() with base 10: '2'
BIndexError: index 2 is out of bounds for axis 0 with size 5
CTypeError: only integer scalar arrays can be converted to a scalar index
DNo error, output: [2 3 4]
Attempts:
2 left
💡 Hint
Check the types inside the indices list used for indexing.
🚀 Application
advanced
2: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]])
Aresult = scores[[0, 2, 4], [1, 3, 1]]
Bresult = scores[:, [1, 3]][[0, 2, 4]]
Cresult = scores[[0, 2, 4], :][:, [1, 3]]
Dresult = scores[[0, 2, 4]][:, [1, 3]]
Attempts:
2 left
💡 Hint
Remember that fancy indexing with two arrays selects elements pairwise, but you want a submatrix.
🧠 Conceptual
expert
2: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?
A(3, 2) because rows[:, None] broadcasts with cols to select elements pairwise
B(3, 4) because arr is 3x4 and indexing preserves shape
C(2, 3) because cols shape is (2,2) and rows shape is (3,)
D(3, 2, 4) because of advanced indexing with 3D output
Attempts:
2 left
💡 Hint
Think about how broadcasting works between rows[:, None] and cols arrays during indexing.