Challenge - 5 Problems
Boolean Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about which numbers in the array are greater than 4.
✗ Incorrect
The code compares each element of the array to 4. Elements 7 and 5 are greater than 4, so their positions are True; others are False.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Count how many elements are less than or equal to 10.
✗ Incorrect
Elements 10, 5, and 0 satisfy the condition, so there are 3 True values.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Look carefully at the comparison operator usage.
✗ Incorrect
The comparison operator '>' is incomplete without a value to compare to, causing a SyntaxError.
🚀 Application
advanced2: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)
Attempts:
2 left
💡 Hint
You want only elements strictly greater than 50.
✗ Incorrect
arr > 50 creates a boolean array True only for elements greater than 50. Using it to index arr filters those elements.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
The boolean array keeps the same shape as the original array.
✗ Incorrect
The comparison creates a boolean array with the same shape as arr, and dtype is bool.