0
0
NumPydata~10 mins

Boolean indexing for filtering 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 filter the array and keep only values greater than 5.

NumPy
import numpy as np
arr = np.array([3, 7, 2, 9, 5])
filtered = arr[arr [1] 5]
print(filtered)
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of > which selects only values equal to 5.
Using < which selects values less than 5.
2fill in blank
medium

Complete the code to filter the array and keep only even numbers.

NumPy
import numpy as np
arr = np.array([1, 4, 7, 8, 10])
even = arr[arr [1] 2 == 0]
print(even)
Drag options to blanks, or click blank then click option'
A+
B//
C**
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using floor division // which does not check for evenness.
Using exponentiation ** which is unrelated.
3fill in blank
hard

Fix the error in the code to filter values less than 10.

NumPy
import numpy as np
arr = np.array([12, 5, 8, 15, 3])
filtered = arr[arr [1] 10]
print(filtered)
Drag options to blanks, or click blank then click option'
A=>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid operator => which causes syntax error.
Using == which selects only values equal to 10.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths for words longer than 3 characters.

NumPy
words = ['cat', 'elephant', 'dog', 'lion']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word > 3 which compares string to number and causes error.
Using word instead of length in the dictionary values.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 4 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 4
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of uppercase for keys.
Filtering with len(w) >= 4 which includes words of length 4.