To compare each element of a NumPy array
arr against the scalar
x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators, use the broadcasting feature with the array as one operand and the scalar as another operand. For example, the greater comparison
arr > x results in an array of Boolean values from the element-wise comparisons.
array > scalar
array >= scalar
array < scalar
array <= scalar
array == scalar
# yields a new Boolean array [True/False ... True/False]
- A NumPy array
- A scalar value
❓ How to compare each element of the NumPy array
arr against the scalar
x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators?
The desired result is a NumPy array of Boolean values respresenting the element-wise comparison results.
For example, consider the following pseudocode of what you’re trying to accomplish:
# Given arr = [1 10 100] x = 3 # Desired res = [1>x 10>x 100>x] = [False True True]
You can use all comparison operators of a scalar value on a NumPy array:
arr > x
- Greater or equal:
arr >= x
arr < x
- Smaller or equal:
arr <= x
arr == x
NumPy will automatically bring both operands into the same shape (a feature called “broadcasting“).
import numpy as np # Given arr = np.array([1, 10, 100]) x = 3 # Greater: print(arr > x) # [False True True] # Greater or equal: print(arr >= x) # [False True True] # Smaller: print(arr < x) # [ True False False] # Smaller or equal: print(arr <= x) # [ True False False] # Equal: print(arr == x) # [False False False]
The comparison is performed element-wise and the result of the operation is a Boolean array as desired.
Data Science Puzzle
import numpy as np # popular instagram accounts # (millions followers) inst = [232, #"@instagram" 133, #"@selenagomez" 59, #"@victoriassecret" 120, #"@cristiano" 111, #"@beyonce" 76] #"@nike" inst = np.array(inst) superstars = inst > 100 print(superstars) print(superstars)
Exercise: What is the output of this puzzle?
You can solve this puzzle on our interactive puzzle-based training app and track your Python skills:
NumPy is a popular Python library for data science focusing on linear algebra.
The following handy NumPy feature will prove useful throughout your career. You can use comparison operators directly on NumPy arrays. The result is an equally-sized NumPy array with Boolean values. Each Boolean indicates whether the comparison evaluates to
True for the respective value in the original array.
The puzzle creates a list of integers. Each integer represents the number of followers of popular Instagram accounts (in millions).
- First, we convert this list to a NumPy array.
- Second, we determine for each account whether it has more than 100 million followers.
We print the first and the third boolean value of the resulting NumPy array. The result is
@victoriassecret with 59 million followers.
Are you a master coder?
Test your skills now!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.