0
0
NumPydata~10 mins

Fancy indexing with integer arrays 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 at positions 1, 3, and 4 from the array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
selected = arr[[1]]
print(selected)
Drag options to blanks, or click blank then click option'
A[2, 3, 4]
B[0, 2, 4]
C[1, 3, 4]
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using indices that are out of range.
Using a tuple instead of a list for indexing.
2fill in blank
medium

Complete the code to select elements at indices stored in the variable 'indices'.

NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25])
indices = np.array([0, 2, 4])
result = arr[[1]]
print(result)
Drag options to blanks, or click blank then click option'
Aindices
Bindices.tolist()
C[indices]
D(indices)
Attempts:
3 left
💡 Hint
Common Mistakes
Wrapping the indices array in another list or tuple.
Converting indices to list unnecessarily.
3fill in blank
hard

Fix the error in the code to correctly select elements at indices 2, 0, and 3.

NumPy
import numpy as np
arr = np.array([7, 14, 21, 28, 35])
indices = [2, 0, 3]
selected = arr[[1]]
print(selected)
Drag options to blanks, or click blank then click option'
Aindices
B(indices)
C[indices]
Dnp.array(indices)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple or list inside another list for indexing.
Not converting the list to a numpy array when needed.
4fill in blank
hard

Fill both blanks to create a new array selecting elements at indices 1 and 4, then multiply them by 10.

NumPy
import numpy as np
arr = np.array([3, 6, 9, 12, 15])
selected = arr[[1]]
result = selected [2] 10
print(result)
Drag options to blanks, or click blank then click option'
A[1, 4]
B*
C+
D[0, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong indices for selection.
Using '+' instead of '*' for multiplication.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping selected elements to their squares for indices 0, 2, and 3.

NumPy
import numpy as np
arr = np.array([2, 4, 6, 8, 10])
squares = { [1]: [2]**2 for [3] in arr[ [0, 2, 3] ] }
print(squares)
Drag options to blanks, or click blank then click option'
Ax
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not using a dictionary comprehension syntax.