import numpy as np
Filtering NumPy Arrays with Boolean IndexingΒΆ
Boolean indexing is one of NumPyβs most practical features for data analysis. A comparison operation like np1 < 5 produces a boolean array of the same shape, where each element is True or False. Passing this boolean array as an index (np1[filtered]) returns only the elements where the condition is True. This replaces verbose loop-and-append patterns with a single, readable expression. In real-world data science, boolean filtering is used constantly β removing outliers, selecting samples that meet criteria, thresholding predictions, and cleaning datasets before model training.
# Filtering Numpy Arrays With Boolean Index Lists
np1 = np.array([1,2,3,4,5,6,7,8,9,10])
#x = [True, True, False, False, False, False, False, False, False, False]
#print(np1[x])
'''
filtered = []
for thing in np1:
if thing % 2 == 0:
filtered.append(True)
else:
filtered.append(False)
print(np1)
print(filtered)
print(np1[filtered])
'''
# Shortcut!
filtered = np1 < 5
print(np1)
print(filtered)
print(np1[filtered])