Boolean masks
A comparison on an array gives an array of True and False, one per element. That boolean array, a mask, is how NumPy filters data without loops.
Masks
Python
import numpy as np
scores = np.array([72, 95, 88, 61, 79, 54])
passed = scores >= 70
print(passed) # [ True True True False True False]
print(scores[passed]) # only where the mask is True
print(scores[scores < 60]) # usually written in one goCounting and testing
Python
import numpy as np
scores = np.array([72, 95, 88, 61, 79, 54])
print((scores >= 70).sum()) # True counts as 1: 4 passed
print((scores >= 70).mean()) # the fraction that passed
print((scores > 90).any()) # is at least one above 90?
print((scores > 50).all()) # is every score above 50?True behaves like 1 and False like 0, so sum counts matches and mean gives the fraction.
Combining conditions
Python
import numpy as np
ages = np.array([15, 22, 37, 45, 63, 71, 19])
print(ages[(ages >= 18) & (ages < 65)]) # and
print(ages[(ages < 18) | (ages >= 65)]) # or
print(ages[~(ages >= 18)]) # notUse &, | and ~, not and, or and not, and put brackets around every condition: & binds more tightly than >=, so ages >= 18 & ages < 65 would be evaluated wrongly.
np.where: choose between two values
Python
import numpy as np
scores = np.array([72, 95, 88, 61, 79, 54])
print(np.where(scores >= 70, "pass", "fail"))
temps = np.array([-3, 5, 12, -8, 0])
print(np.where(temps < 0, 0, temps)) # negative readings become 0
print(np.where(scores >= 90)) # with one argument: the positions where it's Truenp.where(condition, if_true, if_false) builds a new array element by element.
Replacing values
Python
import numpy as np
readings = np.array([12.5, -999.0, 13.1, 12.8, -999.0, 14.0]) # -999 means "sensor failed"
readings[readings == -999.0] = np.nan # mark them missing
print(readings)
print(np.nanmean(readings)) # the mean, ignoring missing values
marks = np.array([105, 88, -4, 72, 99])
print(np.clip(marks, 0, 100)) # force values into 0..100Assigning to a masked selection changes those elements in place. np.nan means "not a number", the standard marker for missing values, and the nan... functions such as np.nanmean skip over it.
Masks on 2D arrays
Python
import numpy as np
sales = np.array([[120, 80, 150],
[ 90, 200, 60],
[ 30, 110, 170]])
print(sales[sales > 100]) # matching values, flattened into 1D
print(np.argwhere(sales > 150)) # their (row, column) positions
big_rows = sales.sum(axis=1) > 320
print(sales[big_rows]) # whole rows whose total is over 320Finished this lesson?Mark it complete to earn +20 XP and move your progress on.