0
0
NumPydata~20 mins

Indexing with ellipsis in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ellipsis Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ellipsis indexing on 3D array
What is the output of the following code snippet?
NumPy
import numpy as np
arr = np.arange(27).reshape(3,3,3)
result = arr[..., 1]
print(result)
A
[[ 0  3  6]
 [ 9 12 15]
 [18 21 24]]
B
[[ 9 10 11]
 [12 13 14]
 [15 16 17]]
C
[[ 1  4  7]
 [10 13 16]
 [19 22 25]]
D
[[ 2  5  8]
 [11 14 17]
 [20 23 26]]
Attempts:
2 left
💡 Hint
Ellipsis (...) replaces all missing full slices in the indexing.
data_output
intermediate
1:30remaining
Shape after ellipsis indexing
Given a 4D numpy array with shape (2,3,4,5), what is the shape of the result after indexing with arr[1, ..., 2]?
NumPy
import numpy as np
arr = np.zeros((2,3,4,5))
result = arr[1, ..., 2]
print(result.shape)
A(3, 4, 5, 1)
B(3, 4)
C(3, 4, 5)
D(3, 5)
Attempts:
2 left
💡 Hint
Ellipsis fills in the missing dimensions. The fixed indices reduce dimensions accordingly.
🔧 Debug
advanced
1:30remaining
Identify the error in ellipsis indexing
What error does the following code raise?
NumPy
import numpy as np
arr = np.arange(24).reshape(2,3,4)
result = arr[..., 4]
print(result)
ANo error, prints a 2D array
BTypeError: ellipsis cannot be used with integer index
CValueError: too many indices for array
DIndexError: index 4 is out of bounds for axis 2 with size 4
Attempts:
2 left
💡 Hint
Check the size of the last dimension and the index used.
🚀 Application
advanced
1:30remaining
Using ellipsis to select all but one dimension
You have a 5D numpy array with shape (2,3,4,5,6). Which indexing expression selects all elements in the first four dimensions but only the element at index 2 in the last dimension?
Aarr[..., 2]
Barr[:, :, :, :, 2]
Carr[2, ...]
Darr[2]
Attempts:
2 left
💡 Hint
Ellipsis replaces multiple full slices.
🧠 Conceptual
expert
2:00remaining
Ellipsis behavior with mixed indexing
Consider a numpy array arr with shape (3,4,5,6). What is the shape of arr[1, ..., 2:4]?
NumPy
import numpy as np
arr = np.zeros((3,4,5,6))
result = arr[1, ..., 2:4]
print(result.shape)
A(4, 5, 2)
B(4, 5, 6)
C(4, 5)
D(5, 6)
Attempts:
2 left
💡 Hint
Ellipsis fills missing dimensions. Slicing keeps dimension size.