0
0
NumPydata~20 mins

Boolean indexing 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 a NumPy Array
What is the output of this code snippet using Boolean indexing on a NumPy array?
NumPy
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
result = arr[arr > 20]
print(result)
A[15 20 25]
B[25 30]
C[10 15 20]
D[20 25 30]
Attempts:
2 left
💡 Hint
Remember, Boolean indexing selects elements where the condition is True.
data_output
intermediate
2:00remaining
Number of Elements Selected by Boolean Indexing
How many elements are selected by the Boolean indexing in this code?
NumPy
import numpy as np
arr = np.array([5, 12, 17, 9, 3, 21])
selected = arr[(arr >= 10) & (arr < 20)]
print(len(selected))
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Count elements between 10 (inclusive) and 20 (exclusive).
Predict Output
advanced
2:00remaining
Output of Boolean Indexing with Multiple Conditions
What is the output of this code using Boolean indexing with multiple conditions?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
result = arr[(arr % 2 == 0) | (arr > 7)]
print(result)
A[2 4 6 8 9]
B[8 9]
C[2 4 6 8]
D[1 3 5 7]
Attempts:
2 left
💡 Hint
Select elements that are even or greater than 7.
🔧 Debug
advanced
2:00remaining
Identify the Error in Boolean Indexing Code
What error does this code raise when using Boolean indexing?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mask = arr > 3
result = arr[mask == True]
print(result)
ANo error, output: [4 5]
BIndexError
CTypeError
DValueError
Attempts:
2 left
💡 Hint
Check if comparing Boolean array to True affects indexing.
🚀 Application
expert
3:00remaining
Filter Rows in 2D Array Using Boolean Indexing
Given this 2D NumPy array, which option correctly selects rows where the second column is less than 5?
NumPy
import numpy as np
arr = np.array([[1, 4, 7], [2, 5, 8], [3, 3, 9], [4, 6, 10]])
result = ???
print(result)
Aarr[:, arr[1, :] < 5]
Barr[arr[1, :] < 5]
Carr[arr[:, 1] < 5]
Darr[arr[:, 1] > 5]
Attempts:
2 left
💡 Hint
Use Boolean indexing on rows based on the second column values.