Just as you can perform arithmetic operations on NumPy arrays, you can also perform logical comparisons. These operations are fundamental for filtering data, checking conditions, and making decisions within your data analysis workflows. Like arithmetic operations, logical operations in NumPy are typically applied element-wise, resulting in arrays of boolean values (True
or False
).
You can compare entire arrays with scalar values or compare two arrays element by element using standard Python comparison operators. NumPy overloads these operators to work efficiently on arrays.
The available comparison operators are:
==
(equal to)!=
(not equal to)<
(less than)>
(greater than)<=
(less than or equal to)>=
(greater than or equal to)Let's see how these work.
Comparing an Array with a Scalar
When you compare a NumPy array with a single number (a scalar), NumPy performs the comparison between the scalar and each element of the array, returning a new boolean array of the same shape.
import numpy as np
# Create a sample array
ages = np.array([25, 18, 65, 40, 12])
# Check which ages are greater than 21
is_adult = ages > 21
print(is_adult)
# Expected Output: [ True False True True False]
# Check which ages are exactly 18
is_eighteen = ages == 18
print(is_eighteen)
# Expected Output: [False True False False False]
The result is_adult
is a boolean array indicating True
where the condition age > 21
was met and False
otherwise.
Comparing Two Arrays
You can also compare two arrays, provided they have compatible shapes (either the same shape or shapes compatible according to broadcasting rules, which we'll discuss later). The comparison is performed element by element.
# Create two arrays of scores
scores1 = np.array([85, 92, 78, 88])
scores2 = np.array([85, 90, 80, 88])
# Check where scores are equal
equal_scores = scores1 == scores2
print(equal_scores)
# Expected Output: [ True False False True]
# Check where scores1 is less than scores2
lower_scores = scores1 < scores2
print(lower_scores)
# Expected Output: [False False True False]
Each element in the resulting boolean array corresponds to the comparison result of the elements at the same position in the original arrays.
Often, you'll need to check multiple conditions simultaneously. For instance, you might want to find elements that are greater than a lower bound and less than an upper bound. NumPy uses element-wise logical operators for this:
&
(logical AND)|
(logical OR)~
(logical NOT)Important: Use the bitwise operators
&
,|
, and~
for element-wise logical operations on NumPy arrays, not the Python keywordsand
,or
, andnot
. The keywordsand
andor
evaluate the truthiness of entire objects, while&
and|
perform element-wise comparisons on the boolean values within the arrays. Also, remember to use parentheses()
around individual comparisons due to operator precedence.
Let's combine conditions using the ages
array from before:
# Find ages between 20 and 50 (exclusive of 20, inclusive of 50)
working_age = (ages > 20) & (ages <= 50)
print(working_age)
# Original ages: [25, 18, 65, 40, 12]
# Expected Output: [ True False False True False]
# Find ages younger than 18 OR older than 60
young_or_senior = (ages < 18) | (ages > 60)
print(young_or_senior)
# Original ages: [25, 18, 65, 40, 12]
# Expected Output: [False False True False True]
# Find ages NOT equal to 18
not_eighteen = ~(ages == 18)
# This is equivalent to: not_eighteen = ages != 18
print(not_eighteen)
# Original ages: [25, 18, 65, 40, 12]
# Expected Output: [ True False True True True]
These combined boolean arrays are extremely useful for selecting data that meets multiple criteria, a technique often called boolean indexing (which you encountered in Chapter 3).
any()
and all()
Sometimes, you don't need the element-wise result, but rather a summary: does any element satisfy the condition, or do all elements satisfy it? NumPy provides the any()
and all()
methods for arrays (and also as standalone functions np.any()
, np.all()
) for this purpose.
any()
: Returns True
if at least one element in the boolean array is True
.all()
: Returns True
only if all elements in the boolean array are True
.# Create a boolean array
results = np.array([True, False, True, True])
# Check if any result is True
print(results.any())
# Expected Output: True
# Check if all results are True
print(results.all())
# Expected Output: False
# Using our ages array again
ages = np.array([25, 18, 65, 40, 12])
# Are there any teenagers (age < 20)?
print((ages < 20).any())
# Expected Output: True (because 18 and 12 are less than 20)
# Are all ages 18 or older?
print((ages >= 18).all())
# Expected Output: False (because 12 is less than 18)
These functions are helpful for quick checks and assertions about your data. You can also apply any()
and all()
along specific axes in multi-dimensional arrays, similar to statistical functions like sum()
or mean()
.
Logical operations form a core part of data selection and manipulation. The boolean arrays they produce act as powerful masks for filtering and modifying your datasets based on complex conditions. As you work with larger datasets, using NumPy's vectorized logical operations will be significantly more efficient than writing explicit Python loops.
© 2025 ApX Machine Learning