NumPy Boolean Masks & Conditional Selection

What is a boolean mask?

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.

Create masks and filter 1D arrays

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]

Conditional replace with np.where

Use 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']

Logical functions vs operators

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]))

Masking 2D arrays (rows and columns)

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]

NaN-safe filters

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)

In-place updates with masks

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]

Performance notes

  • Masks and np.where are vectorized and avoid Python loops.
  • Boolean masks create temporary arrays; for very large data, profile memory/time.
  • When chaining many conditions, consider building masks step-by-step for clarity.

Practice: quick exercises

# 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)
Numpy Indexing & Slicing where() array() shape reshape()


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer