0
0
NumPydata~10 mins

Multi-dimensional fancy indexing 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 elements from a 2D array using fancy indexing.

NumPy
import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
result = arr[[1], [0, 2]]
Drag options to blanks, or click blank then click option'
A[1, 2]
B1
C2
D[0, 2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single integer instead of a list for row indices.
Confusing row and column indices.
2fill in blank
medium

Complete the code to select elements from a 3x3 array using fancy indexing with two arrays.

NumPy
import numpy as np
arr = np.arange(9).reshape(3, 3)
result = arr[[1], [0, 2, 1]]
Drag options to blanks, or click blank then click option'
A[1, 2, 0]
B[0, 1, 2]
C[2, 1, 0]
D[0, 0, 0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using row indices of different length than column indices.
Using a single integer instead of a list for row indices.
3fill in blank
hard

Fix the error in the code to correctly select elements using multi-dimensional fancy indexing.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
result = arr[[0, 2], [1]]
Drag options to blanks, or click blank then click option'
A[0, 1]
B[1, 0]
C[1, 1]
D[2, 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using column indices that do not match the length of row indices.
Using invalid column indices.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = { [1] : len([2]) for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Bwords
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list 'words' instead of the variable 'word'.
Using a string literal instead of a variable.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = { [1] : [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word as key without converting to uppercase.
Using incorrect comparison operators.