0
0
NumPydata~20 mins

Multi-dimensional fancy indexing in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-dimensional Fancy Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multi-dimensional fancy indexing with arrays
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, 60], [70, 80, 90]])
rows = np.array([0, 1, 2])
cols = np.array([2, 1, 0])
result = arr[rows, cols]
print(result)
A[30 50 70]
B[10 50 90]
C[30 40 90]
D[10 20 30]
Attempts:
2 left
💡 Hint
Remember that arr[rows, cols] selects elements at positions (rows[i], cols[i]) for each i.
data_output
intermediate
2:00remaining
Shape of result from multi-dimensional fancy indexing
Given the code below, what is the shape of the resulting array 'result'?
NumPy
import numpy as np
arr = np.arange(27).reshape(3,3,3)
rows = np.array([0, 1])
cols = np.array([1, 2])
result = arr[rows, cols]
print(result.shape)
A(2, 2)
B(2, 3)
C(3, 3)
D(3, 2)
Attempts:
2 left
💡 Hint
Check how fancy indexing with two arrays affects the shape when indexing a 3D array.
🔧 Debug
advanced
2:00remaining
Identify the error in multi-dimensional fancy indexing
What error does the following code raise when executed?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
rows = np.array([0, 1, 2])
cols = np.array([1, 0, 1])
result = arr[rows, cols]
print(result)
ANo error, outputs [2 3 4]
BValueError: shape mismatch between rows and cols
CTypeError: unsupported operand type(s) for indexing
DIndexError: index 2 is out of bounds for axis 0 with size 2
Attempts:
2 left
💡 Hint
Check the size of the array and the indices used for rows.
🚀 Application
advanced
2:00remaining
Extract diagonal elements using multi-dimensional fancy indexing
Which option correctly extracts the diagonal elements from a 2D NumPy array 'arr' using fancy indexing?
NumPy
import numpy as np
arr = np.array([[5, 6, 7], [8, 9, 10], [11, 12, 13]])
Aarr[np.arange(3), np.arange(3)]
Barr[[0,1,2], [2,1,0]]
Carr[:, np.arange(3)]
Darr[np.arange(3), 0]
Attempts:
2 left
💡 Hint
Diagonal elements have the same row and column indices.
🧠 Conceptual
expert
2:30remaining
Effect of broadcasting in multi-dimensional fancy indexing
Given the code below, what is the shape of 'result' and why?
NumPy
import numpy as np
arr = np.arange(24).reshape(4,6)
rows = np.array([[0], [1], [2]])
cols = np.array([1, 3, 5])
result = arr[rows, cols]
print(result.shape)
A(1, 3) because rows is 2D and cols is 1D, resulting in minimal broadcast
B(4, 6) because fancy indexing returns the original array shape
C(3, 3) because rows and cols broadcast to shape (3,3) and select elements accordingly
D(3,) because rows and cols are flattened before indexing
Attempts:
2 left
💡 Hint
Consider how NumPy broadcasts arrays of shapes (3,1) and (3,) for indexing.