0
0
NumPydata~5 mins

Boolean indexing for filtering in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly elements where the condition is False
BOnly elements where the condition is True
CAll elements regardless of condition
DThe original array unchanged
How do you create a Boolean mask for elements equal to 10 in a numpy array arr?
Amask = arr < 10
Bmask = arr = 10
Cmask = arr != 10
Dmask = arr == 10
If arr = np.array([2, 5, 7]), what is the result of arr[arr < 5]?
Aarray([2])
Barray([5, 7])
Carray([2, 5])
Darray([7])
Can Boolean indexing be combined with multiple conditions in numpy?
ANo, only one condition is allowed
BYes, but only with or operator
CYes, using &amp; and | operators with parentheses
DNo, Boolean indexing does not support conditions
What shape must a Boolean mask have to filter a numpy array?
AThe same shape as the array
BAny shape smaller than the array
COnly 1D shape
DShape does not matter
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.