0
0
NumPydata~10 mins

Combining fancy and slice 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 rows 1 and 3, and columns 2 to 4 from the array.

NumPy
import numpy as np
arr = np.arange(20).reshape(5,4)
result = arr[[1], 2:4]
print(result)
Drag options to blanks, or click blank then click option'
A[0, 2]
B[2, 4]
C[1, 2]
D[1, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a slice instead of a list for row indices.
Selecting columns outside the array shape.
2fill in blank
medium

Complete the code to select rows 0, 2, 4 and columns 1 and 3 from the array.

NumPy
import numpy as np
arr = np.arange(20).reshape(5,4)
result = arr[[1], [1, 3]]
print(result)
Drag options to blanks, or click blank then click option'
A[0, 1, 2]
B[0, 2, 4]
C[1, 3, 5]
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range instead of a list for rows.
Selecting columns with a slice instead of a list.
3fill in blank
hard

Fix the error in the code to select rows 1 and 2, and columns 0 to 2.

NumPy
import numpy as np
arr = np.arange(15).reshape(3,5)
result = arr[[1], 0:3]
print(result)
Drag options to blanks, or click blank then click option'
A[1, 2]
B1, 2
Cslice(1,3)
Dnp.array([1, 2])
Attempts:
3 left
💡 Hint
Common Mistakes
Using comma-separated indices instead of a list.
Using slice object where a list is expected.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths if length > 3.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using less than (<) instead of greater than (>) in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.

NumPy
data = {'a': 1, 'b': -2, 'c': 3}
result = [1]: [2] for k, v in data.items() if v [3] 0}
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys instead of uppercase keys.
Using less than (<) instead of greater than (>) in the condition.
Using keys as values instead of values.