0
0
NumPydata~10 mins

Indexing with ellipsis in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all elements in the last dimension of a 3D NumPy array using ellipsis.

NumPy
import numpy as np
arr = np.arange(27).reshape(3, 3, 3)
result = arr[1]
Drag options to blanks, or click blank then click option'
A[..., :]
B[..., 1]
C[:, :, :]
D[0, ...]
Attempts:
3 left
💡 Hint
Common Mistakes
Using explicit slicing for all dimensions instead of ellipsis.
Using an index instead of a slice after ellipsis.
2fill in blank
medium

Complete the code to select the first element in the last dimension for all other dimensions using ellipsis.

NumPy
import numpy as np
arr = np.arange(24).reshape(2, 3, 4)
result = arr[1]
Drag options to blanks, or click blank then click option'
A[..., 0]
B[0, ..., 0]
C[..., 1]
D[:, :, 0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using explicit indices for all dimensions instead of ellipsis.
Selecting the wrong index in the last dimension.
3fill in blank
hard

Fix the error in the code to correctly select all elements in the last two dimensions using ellipsis.

NumPy
import numpy as np
arr = np.arange(60).reshape(3, 4, 5)
result = arr[1]
Drag options to blanks, or click blank then click option'
A[..., ::]
B[..., 0]
C[..., :, :]
D[..., 1:]
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid slice syntax after ellipsis.
Selecting only one dimension instead of two.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword.startswith('a')
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Filtering with incorrect conditions.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word in uppercase to its length only if the length is greater than 2.

NumPy
words = ['cat', 'dog', 'a', 'elephant']
result = [1]: [2] for word in words if [3]
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 2
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Not filtering words by length correctly.