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 array for indexing?
You create a Boolean array by applying a condition to a numpy array, for example: arr > 5 returns True for elements greater than 5 and False otherwise.
Click to reveal answer
beginner
Example: Select all elements greater than 10 from array arr = np.array([5, 12, 7, 15])
Use Boolean indexing: arr[arr > 10] returns array([12, 15]) because only 12 and 15 are greater than 10.
Click to reveal answer
intermediate
Can Boolean indexing be used to modify elements in a numpy array?
Yes, you can use Boolean indexing to select elements and assign new values. For example, arr[arr < 5] = 0 sets all elements less than 5 to zero.
Click to reveal answer
intermediate
What happens if the Boolean array has a different shape than the original array?
The Boolean array must have the same shape as the original array for Boolean indexing to work. Otherwise, numpy will raise a ValueError.
Click to reveal answer
What does arr[arr > 3] do if arr = np.array([1, 4, 3, 6])?
AReturns the original array
BSelects elements greater than 3
CSelects elements equal to 3
DSelects elements less than 3
✗ Incorrect
arr > 3 creates a Boolean array where elements greater than 3 are True. Using this to index arr selects those elements.
Which of these is a valid Boolean indexing expression?
Aarr[arr == 5]
Barr[5]
Carr["5"]
Darr[5 > arr]
✗ Incorrect
Both A (arr == 5) and D (5 > arr) return Boolean arrays, which can be used to index arr. B is integer indexing, C is invalid.
What type of array is needed for Boolean indexing?
ABoolean array
BFloat array
CInteger array
DString array
✗ Incorrect
Boolean indexing requires a Boolean array where each element is True or False.
If arr = np.array([2, 8, 5]), what does arr[arr < 5] = 0 do?
ADeletes elements less than 5
BSets elements greater than 5 to zero
CRaises an error
DSets elements less than 5 to zero
✗ Incorrect
Boolean indexing selects elements less than 5 and assigns zero to them.
What error occurs if Boolean array shape does not match original array?
AIndexError
BTypeError
CValueError
DKeyError
✗ Incorrect
Numpy raises a ValueError if the Boolean array shape is different from the original array shape.
Explain how Boolean indexing works in numpy with an example.
Think about how True/False values select elements.
You got /4 concepts.
Describe how you can use Boolean indexing to modify elements in a numpy array.
Consider changing values where condition is True.
You got /3 concepts.
Practice
(1/5)
1. What does boolean indexing in numpy allow you to do?
easy
A. Create a new array filled with zeros
B. Change the data type of an array
C. Sort the array elements in ascending order
D. Select elements from an array using a True/False mask
Solution
Step 1: Understand boolean indexing concept
Boolean indexing uses a True/False array to pick elements from another array.
Step 2: Identify the correct operation
Only Select elements from an array using a True/False mask describes selecting elements based on True/False values.
Final Answer:
Select elements from an array using a True/False mask -> Option D
Quick Check:
Boolean indexing = Select elements [OK]
Hint: Boolean mask picks elements where True appears [OK]
Common Mistakes:
Confusing boolean indexing with sorting
Thinking it changes data types
Assuming it creates new arrays with zeros
2. Which of the following is the correct syntax to select elements greater than 5 from a numpy array arr using boolean indexing?
easy
A. arr[arr < 5]
B. arr > 5[arr]
C. arr[arr > 5]
D. arr.select(arr > 5)
Solution
Step 1: Understand boolean indexing syntax
The correct syntax is to use the array with a boolean mask inside square brackets: arr[condition].
Step 2: Check each option
arr[arr > 5] uses arr[arr > 5], which is correct. Others have wrong order or invalid methods.
Final Answer:
arr[arr > 5] -> Option C
Quick Check:
Correct syntax = arr[condition] [OK]
Hint: Put condition inside brackets after array name [OK]
Common Mistakes:
Placing condition before array name
Using invalid methods like select()
Using wrong comparison operators
3. What is the output of the following code?
import numpy as np
arr = np.array([2, 7, 4, 9, 1])
result = arr[arr % 2 == 1]
medium
A. [7 9 1]
B. [2 4]
C. [7 4 9]
D. [2 7 4 9 1]
Solution
Step 1: Understand the condition arr % 2 == 1
This condition selects elements where the remainder when divided by 2 is 1, i.e., odd numbers.
Step 2: Apply condition to array elements
Elements 7, 9, and 1 are odd, so they are selected.
Final Answer:
[7 9 1] -> Option A
Quick Check:
Odd elements = [7 9 1] [OK]
Hint: Filter with modulo to get odd numbers [OK]
Common Mistakes:
Selecting even numbers instead
Confusing modulo operator
Returning full array instead of filtered
4. The following code has a problem. What is the problem?
C. Comparison operator > is invalid for numpy arrays
D. Square brackets cannot be used for indexing
Solution
Step 1: Analyze the boolean mask creation
mask = arr > 2 creates a boolean array correctly.
Step 2: Check the indexing expression
Using mask == True is redundant since mask is already boolean. It's unnecessary and bad practice.
Step 3: Correct usage
Use arr[mask] directly without comparing mask to True.
Final Answer:
mask == True is unnecessary and redundant -> Option A
Quick Check:
Use boolean mask directly [OK]
Hint: Use mask directly, no need for '== True' [OK]
Common Mistakes:
Adding '== True' to boolean mask
Trying to convert numpy array to list for indexing
Misunderstanding comparison operators
5. Given a numpy array data = np.array([10, 15, 20, 25, 30]), how can you select elements greater than 12 and less than 28 using boolean indexing?
hard
A. data[data > 12 and data < 28]
B. data[(data > 12) & (data < 28)]
C. data[data > 12 | data < 28]
D. data[(data > 12) or (data < 28)]
Solution
Step 1: Understand combining conditions with & and parentheses
In numpy, use & for element-wise AND and wrap each condition in parentheses.
Step 2: Check each option
data[(data > 12) & (data < 28)] correctly uses & (element-wise AND) with parentheses around each condition. Using Python keywords and/or cause errors because they don't work element-wise on arrays. Using | (bitwise OR) selects elements greater than 12 or less than 28, which is wrong here.
Final Answer:
data[(data > 12) & (data < 28)] -> Option B
Quick Check:
Use & with parentheses for AND conditions [OK]
Hint: Use & with parentheses for multiple conditions [OK]
Common Mistakes:
Using 'and' or 'or' instead of & or |
Not using parentheses around conditions
Using | (OR) instead of & (AND) for filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.