0
0
NumPydata~20 mins

Creating boolean arrays in NumPy - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of boolean array creation with comparison
What is the output of this code snippet that creates a boolean array by comparing elements of a numpy array to a value?
NumPy
import numpy as np
arr = np.array([3, 7, 1, 5])
result = arr > 4
print(result)
A[True False True False]
B[False True False True]
C[False False False False]
D[True True True True]
Attempts:
2 left
💡 Hint
Think about which numbers in the array are greater than 4.
data_output
intermediate
2:00remaining
Count of True values in a boolean array
Given a boolean array created by comparing elements of a numpy array, how many True values does it contain?
NumPy
import numpy as np
arr = np.array([10, 15, 20, 5, 0])
bool_arr = arr <= 10
count_true = np.sum(bool_arr)
print(count_true)
A4
B2
C1
D3
Attempts:
2 left
💡 Hint
Count how many elements are less than or equal to 10.
🔧 Debug
advanced
2:00remaining
Identify the error in boolean array creation
What error does this code raise when trying to create a boolean array by comparing elements of a numpy array?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr >
print(result)
ASyntaxError
BTypeError
CNameError
DIndexError
Attempts:
2 left
💡 Hint
Look carefully at the comparison operator usage.
🚀 Application
advanced
2:00remaining
Filter array elements using a boolean array
Which option correctly filters elements greater than 50 from the numpy array using a boolean array?
NumPy
import numpy as np
arr = np.array([10, 60, 30, 80, 50])
filtered = ???
print(filtered)
Aarr[arr < 50]
Barr[arr == 50]
Carr[arr > 50]
Darr[arr >= 50]
Attempts:
2 left
💡 Hint
You want only elements strictly greater than 50.
🧠 Conceptual
expert
2:00remaining
Understanding boolean array shape and dtype
Given a 2D numpy array, what is the shape and dtype of the boolean array created by comparing each element to zero?
NumPy
import numpy as np
arr = np.array([[1, -1], [0, 2]])
bool_arr = arr > 0
print(bool_arr.shape, bool_arr.dtype)
A(2, 2) bool
B(4,) int
C(2, 2) int
D(4,) bool
Attempts:
2 left
💡 Hint
The boolean array keeps the same shape as the original array.