Recall & Review
beginner
What is Boolean indexing in numpy?
Boolean indexing is a way to select elements from a numpy array using a condition that returns True or False for each element. Only elements where the condition is True are selected.
Click to reveal answer
beginner
How do you create a Boolean mask to filter values greater than 5 in a numpy array named
arr?You write
mask = arr > 5. This creates an array of True/False values where True means the element is greater than 5.Click to reveal answer
beginner
What happens when you use a Boolean mask to index a numpy array?
The array returns only the elements where the mask is True, effectively filtering the array based on the condition.
Click to reveal answer
beginner
Example: Given
arr = np.array([1, 4, 6, 8]), what does arr[arr > 5] return?It returns
array([6, 8]) because only 6 and 8 are greater than 5.Click to reveal answer
intermediate
Can Boolean indexing be used with multi-dimensional numpy arrays?
Yes, Boolean indexing works with multi-dimensional arrays. The mask must have the same shape as the array, and it filters elements accordingly.
Click to reveal answer
What does Boolean indexing return when applied to a numpy array?
✗ Incorrect
Boolean indexing filters the array and returns only elements where the condition evaluates to True.
How do you create a Boolean mask for elements equal to 10 in a numpy array
arr?✗ Incorrect
The correct syntax for equality comparison is
arr == 10, which creates a Boolean mask.If
arr = np.array([2, 5, 7]), what is the result of arr[arr < 5]?✗ Incorrect
Only 2 is less than 5, so the filtered array contains just 2.
Can Boolean indexing be combined with multiple conditions in numpy?
✗ Incorrect
Multiple conditions can be combined using & (and) and | (or) with parentheses to group conditions.
What shape must a Boolean mask have to filter a numpy array?
✗ Incorrect
The Boolean mask must have the same shape as the array to correctly filter elements.
Explain how Boolean indexing works in numpy and give a simple example.
Think about how True/False values select elements.
You got /4 concepts.
Describe how to filter a numpy array with multiple conditions using Boolean indexing.
Remember to use parentheses around each condition.
You got /3 concepts.