0
0
NumPydata~20 mins

Boolean indexing for filtering in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Boolean Indexing on NumPy Array
What is the output of the following code snippet using NumPy boolean indexing?
NumPy
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
filtered = arr[arr > 20]
print(filtered)
A[20 25 30]
B[15 20 25 30]
C[25 30]
D[10 15 20]
Attempts:
2 left
💡 Hint
Remember that boolean indexing selects elements where the condition is True.
data_output
intermediate
2:00remaining
Number of Elements After Boolean Filtering
How many elements remain after applying the boolean filter in the code below?
NumPy
import numpy as np
arr = np.array([5, 12, 17, 9, 3, 21, 14])
filtered = arr[(arr >= 10) & (arr <= 20)]
print(len(filtered))
A3
B5
C4
D2
Attempts:
2 left
💡 Hint
Count elements between 10 and 20 inclusive.
🔧 Debug
advanced
2:00remaining
Identify the Error in Boolean Indexing
What error will the following code produce?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
filtered = arr[arr > 2 and arr < 5]
print(filtered)
ASyntaxError
BTypeError
CValueError
DNo error, outputs [3 4]
Attempts:
2 left
💡 Hint
Check how logical operators work with NumPy arrays.
🚀 Application
advanced
2:00remaining
Filter Rows in 2D NumPy Array Using Boolean Indexing
Given a 2D NumPy array representing data, which option correctly filters rows where the second column is greater than 50?
NumPy
import numpy as np
data = np.array([[10, 45], [20, 55], [30, 65], [40, 35]])
filtered = ???
print(filtered)
Adata[data[:, 1] > 50]
Bdata[data[1] > 50]
Cdata[:, data[1] > 50]
Ddata[:, 1] > 50
Attempts:
2 left
💡 Hint
Use boolean indexing on rows by selecting the column first.
🧠 Conceptual
expert
3:00remaining
Understanding Boolean Indexing Behavior with NaN Values
Consider the following NumPy array with NaN values. What will be the output of the boolean indexing operation?
NumPy
import numpy as np
arr = np.array([1.0, np.nan, 3.0, np.nan, 5.0])
filtered = arr[arr > 2]
print(filtered)
ARaises ValueError
B[nan 3. nan 5.]
C[3. nan 5.]
D[3. 5.]
Attempts:
2 left
💡 Hint
NaN compared with any number returns False in boolean indexing.