Boolean indexing is the way to get elements from a NumPy array based on conditions.
You simply say we can filter array elements using boolean conditions.
Syntax for Boolean Indexing in NumPy:
new_array = old_array[condition]
For Exmaple:
import numpy as np
profit = np.array([12, 23, 2, 9, 2, 32 ,34])
print("Orignal Profit : ", profit)
Problem: Retrieve all elements that are greater than 15.
Solution:
import numpy as np
profit = np.array([12, 23, 2, 9, 2, 32 ,34])
print("Orignal Profit : ", profit)
new_profit = profit[profit > 15] # filtering data
print("Filtered profit", new_profit)
Output:
Orignal Profit : [12 23 2 9 2 32 34]
Filtered profit [23 32 34]
Let's do the Boolean Indexing with 2D Array for better understanding.
Problem: We have an 2D ndarray. Task is to get all the elements from the array which are divisible by 2.
Solution:
import numpy as np
my_data = np.array([[1, 2, 3, 11], [4, 5, 6, 12], [7, 8, 9,13]])
print("Orignal Array : ", my_data)
new_array = my_data[my_data % 2 == 0]
print(new_array)
Output:
Orignal Array : [[ 1 2 3 11]
[ 4 5 6 12]
[ 7 8 9 13]]
[ 2 4 6 12 8]
Note: Always remember when we apply Boolean indexing on any dimension of array then the new array is always returned the 1-D.
Cool!!
We just understand how to get or filter elements by using conditions.
Going forward, let's see: if we have more than one condition:
Problem: We have a numpy array and want to get all the numbers that are divisible by both 2 and 3. For example, 6 is included because it is divisible by 2 and 3.
Solution: To achieve this we are using bitwise and (&) operator.
Example:
import numpy as np
my_data = np.array([[1, 2, 3, 11], [4, 5, 6, 12], [7, 8, 9, 18]])
print("Orignal Array : ", my_data)
new_array = my_data[(my_data % 2 == 0) & (my_data % 3 == 0)]
print(new_array)
Output:
Orignal Array : [[ 1 2 3 11]
[ 4 5 6 12]
[ 7 8 9 18]]
[ 6 12 18]
10+ Interview Questions for NumPy Boolean Indexing
Question 1: What is Boolean Indexing in NumPy, and how does it work?
Answer: Boolean indexing in NumPy allows us to filter elements of an array based on a condition. It creates a new array containing only the elements that satisfy the given condition.
Example:
import numpy as np
# Create a NumPy array
arr = np.array([10, 20, 30, 40, 50])
# Filter elements greater than 25
filtered = arr[arr > 25]
print(filtered)
Output:
[30 40 50]
Question 2: How would you filter rows in a 2D array where the first column’s values are greater than a certain threshold?
Answer: Use boolean indexing along with conditions on the specified column to filter rows.
Example:
import numpy as np
# Create a 2D array
matrix = np.array([[10, 20], [30, 40], [50, 60], [5, 15]])
# Filter rows where the first column is greater than 20
filtered = matrix[matrix[:, 0] > 20]
print(filtered)
Output:
[[30 40]
[50 60]]
Question 3: What is the difference between &, |, and ~ in boolean indexing?
Answer:
& is used for "AND" conditions.
| is used for "OR" conditions.
~ is used to negate (invert) a condition.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
# Filter elements that are greater than 20 AND less than 50
filtered_and = arr[(arr > 20) & (arr < 50)]
print("AND:", filtered_and)
# Filter elements that are less than 20 OR greater than 40
filtered_or = arr[(arr < 20) | (arr > 40)]
print("OR:", filtered_or)
# Filter elements NOT less than 30
filtered_not = arr[~(arr < 30)]
print("NOT:", filtered_not)
Output:
AND: [30 40]
OR: [10 50]
NOT: [30 40 50]
Question 4: Write code to filter values greater than 5 but less than 15 in a NumPy array.
Answer: Use boolean conditions combined with the & operator.
Example:
import numpy as np
arr = np.array([1, 5, 10, 15, 20])
# Filter elements greater than 5 but less than 15
filtered = arr[(arr > 5) & (arr < 15)]
print(filtered)
Output:
[10]
Question 5: How can you filter all even numbers from a NumPy array using boolean indexing?
Answer: Use the modulo operator % to check for even numbers.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Filter even numbers
filtered = arr[arr % 2 == 0]
print(filtered)
Output:
[2 4 6]
Question 6: How do you check for missing values in a NumPy array using boolean indexing?
Answer: Use np.isnan() to identify missing (NaN) values.
Example:
import numpy as np
arr = np.array([1, np.nan, 3, np.nan, 5])
# Filter missing values
missing_values = arr[np.isnan(arr)]
print(missing_values)
Output:
[nan nan]
Question 7: How can you use boolean indexing to replace elements in a NumPy array?
Answer: Use boolean indexing to locate elements and assign new values to them.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Replace elements greater than 3 with 0
arr[arr > 3] = 0
print(arr)
Output:
[1 2 3 0 0]
Question 8: How do you filter rows in a 2D array where any column value is greater than a threshold?
Answer: Use np.any() with axis=1 for row-wise evaluation.
Example:
import numpy as np
matrix = np.array([[10, 20], [5, 40], [50, 2]])
# Filter rows where any column value > 30
filtered = matrix[np.any(matrix > 30, axis=1)]
print(filtered)
Output:
[[ 5 40]
[50 2]]
Question 9: How would you use boolean indexing to count elements that satisfy a condition?
Answer: Use np.sum() on the boolean array generated by the condition.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Count elements greater than 3
count = np.sum(arr > 3)
print(count)
Output:
2
Question 10: Can you use boolean indexing to extract unique values that meet a condition? How?
Answer: Combine np.unique() with boolean indexing to extract unique values.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 3, 2, 5])
# Extract unique values greater than 2
unique_values = np.unique(arr[arr > 2])
print(unique_values)
Output:
[3 4 5]
Leave a comment
You must be logged in to post a comment.