Challenge - 5 Problems
Boolean Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that boolean indexing selects elements where the condition is True.
✗ Incorrect
The condition arr > 20 selects elements strictly greater than 20, which are 25 and 30.
❓ data_output
intermediate2: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))
Attempts:
2 left
💡 Hint
Count elements between 10 and 20 inclusive.
✗ Incorrect
Elements >=10 and <=20 are 12, 17, 14, so 3 elements.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check how logical operators work with NumPy arrays.
✗ Incorrect
The 'and' operator cannot be used with NumPy arrays for element-wise comparison. It raises a TypeError.
🚀 Application
advanced2: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)
Attempts:
2 left
💡 Hint
Use boolean indexing on rows by selecting the column first.
✗ Incorrect
data[:, 1] selects the second column. Then data[data[:, 1] > 50] selects rows where that condition is True.
🧠 Conceptual
expert3: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)
Attempts:
2 left
💡 Hint
NaN compared with any number returns False in boolean indexing.
✗ Incorrect
NaN compared with > 2 returns False, so NaNs are excluded. Only 3.0 and 5.0 remain.