A boolean mask is an array of True/False values that selects elements from another array. It enables fast filtering and in-place updates without Python loops.
import numpy as np
x = np.array([5, 12, 7, 22, 3, 18])
mask = x >= 10
print(mask) # [False True False True False True]
print(x[mask]) # [12 22 18]
# Combine conditions (use parentheses with & and |)
mask2 = (x >= 10) & (x % 2 == 0)
print(x[mask2]) # [12 22 18]
np.whereUse np.where(cond, a_if_true, a_if_false) to produce a new array by condition:
# Replace values < 10 with 0, keep others
y = np.where(x < 10, 0, x)
print(y) # [ 0 12 0 22 0 18]
# Map to categories
labels = np.where(x >= 15, 'high', 'low')
print(labels) # ['low' 'low' 'low' 'high' 'low' 'high']
For readability you can use either bitwise operators &, |, ~ with parentheses, or the functions np.logical_and, np.logical_or, np.logical_not:
a = np.arange(12)
m1 = (a >= 3) & (a <= 8) # operators
m2 = np.logical_and(a >= 3, a <= 8) # functions
print((a[m1], a[m2])) # (array([3,4,5,6,7,8]), array([3,4,5,6,7,8]))
b = np.array([[ 1, 2, 3],
[10, 20, 30],
[40, 50, 60]])
# Column-wise filter: take rows where column 0 >= 10
row_mask = b[:, 0] >= 10
print(b[row_mask]) # [[10 20 30],
# [40 50 60]]
# Element-wise mask: keep only elements >= 20
elem_mask = b >= 20
print(b[elem_mask]) # [20 30 40 50 60]
Use np.isfinite, np.isnan, np.isinf to build reliable masks when data contains special values.
c = np.array([1.0, np.nan, 3.5, np.inf, -np.inf, 2.2])
finite_mask = np.isfinite(c)
print(c[finite_mask]) # [1. 3.5 2.2]
# Replace NaNs with column mean example (1D demo)
c_fixed = np.where(np.isnan(c), np.nanmean(c), c)
print(c_fixed)
Masks make selective assignment easy and fast.
d = np.arange(10)
d[d % 2 == 1] = -1
print(d) # [ 0 -1 2 -1 4 -1 6 -1 8 -1]
np.where are vectorized and avoid Python loops.# 1) Keep numbers between 25 and 60 inclusive from 0..99
a = np.arange(100)
print(a[(a >= 25) & (a <= 60)])
# 2) From a 5x5 array, set values < the row mean to 0
b = np.arange(25).reshape(5,5)
row_means = b.mean(axis=1, keepdims=True)
b[b < row_means] = 0
print(b)
# 3) Replace NaNs in an array with the median of finite values
c = np.array([2.0, np.nan, 7.0, np.nan, 5.0])
finite = c[np.isfinite(c)]
c = np.where(np.isnan(c), np.median(finite), c)
print(c)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.