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]
Problem Formulation
Given are:
- A NumPy array
arr
. - A scalar value
x
.
β 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]
Solution: Broadcasting
You can use all comparison operators of a scalar value on a NumPy array:
- Greater:
arr > x
- Greater or equal:
arr >= x
- Smaller:
arr < x
- Smaller or equal:
arr <= x
- Equal:
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[0]) print(superstars[2])
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 True
for @instagram
with 232 million followers and False
for @victoriassecret
with 59 million followers.
Are you a master coder?
Test your skills now!
Related Video
Programmer Humor
Q: What is the object-oriented way to become wealthy?
π°
A: Inheritance.