NumPy provides several comparison and logical operations that can be performed on NumPy arrays.
NumPy's comparison operators allow for element-wise comparison of two arrays.
Similarly, logical operators perform boolean algebra, which is a branch of algebra that deals with True
and False
statements.
First we'll discuss comparison operations and then about logical operations in NumPy.
NumPy Comparison Operators
NumPy provides various element-wise comparison operators that can compare the elements of two NumPy arrays.
Here's a list of various comparison operators available in NumPy.
Operators | Descriptions |
---|---|
< (less than) |
returns True if element of the first array is less than the second one |
<= (less than or equal to) |
returns True if element of the first array is less than or equal to the second one |
> (greater than) |
returns True if element of the first array is greater than the second one |
>= (greater than or equal to) |
returns True if element of the first array is greater than or equal to the second one |
== (equal to) |
returns True if the element of the first array is equal to the second one |
!= (not equal to) |
returns True if the element of the first array is not equal to the second one |
Next, we'll see examples of these operators.
Example 1: NumPy Comparison Operators
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([3, 2, 1])
# less than operator
result1 = array1 < array2
print("array1 < array2:",result1) # Output: [ True False False]
# greater than operator
result2 = array1 > array2
print("array1 > array2:",result2) # Output: [False False True]
# equal to operator
result3 = array1 == array2
print("array1 == array2:",result3) # Output: [False True False]
Output
array1 < array2: [ True False False] array1 > array2: [False False True] array1 == array2: [False True False]
Here, we can see that the output of the comparison operators is also an array, where each element is either True
or False
based on the array element's comparison.
NumPy Comparison Functions
NumPy also provides built-in functions to perform all the comparison operations.
For example, the less()
function returns True
if each element of the first array is less than the corresponding element in the second array.
Here's a list of all built-in comparison functions.
Functions | Descriptions |
---|---|
less() |
returns element-wise True if the first value is less than the second |
less_equal() |
returns element-wise True if the first value is less than or equal to second |
greater() |
returns element-wise True if the first value is greater then second |
greater_equal() |
returns element-wise True if the first value is greater than or equal to second |
equal() |
returns element-wise True if two values are equal |
not_equal() |
returns element-wise True if two values are not equal |
Next, we will see an example of all these functions.
Example 2: NumPy Comparison Functions
import numpy as np
array1 = np.array([9, 12, 21])
array2 = np.array([21, 12, 9])
# use of less()
result = np.less(array1, array2)
print("Using less():",result) # Output: [ True False False]
# use of less_equal()
result = np.less_equal(array1, array2)
print("Using less_equal():",result) # Output: [ True True False]
# use of greater()
result = np.greater(array1, array2)
print("Using greater():",result) # Output: [False False True]
# use of greater_equal()
result = np.greater_equal(array1, array2)
print("Using greater_equal():",result) # Output: [False True True]
# use of equal()
result = np.equal(array1, array2)
print("Using equal():",result) # Output: [False True False]
# use of not_equal()
result = np.not_equal(array1, array2)
print("Using not_equal():",result) # Output: [ True False True]
Output
Using less(): [ True False False] Using less_equal(): [ True True False] Using greater(): [False False True] Using greater_equal(): [False True True] Using equal(): [False True False] Using not_equal(): [ True False True]
NumPy Logical Operations
As mentioned earlier, logical operators perform Boolean algebra; a branch of algebra that deals with True
and False
statements.
Logical operations are performed element-wise. For example, if we have two arrays x1
and x2
of the same shape, the output of the logical operator will also be an array of the same shape.
Here's a list of various logical operators available in NumPy:
Operators | Descriptions |
---|---|
logical_and |
Computes the element-wise truth value of x1 AND x2 |
logical_or |
Computes the element-wise truth value of x1 OR x2 |
logical_not |
Computes the element-wise truth value of NOT x |
Next, we will see examples of these operators.
Example 3: NumPy Logical Operations
import numpy as np
x1 = np.array([True, False, True])
x2 = np.array([False, False, True])
# Logical AND
print(np.logical_and(x1, x2)) # Output: [False False True]
# Logical OR
print(np.logical_or(x1, x2)) # Output: [ True False True]
# Logical NOT
print(np.logical_not(x1)) # Output: [False True False]
Here, we have performed logical operations on two arrays, x1 and x2, containing boolean values.
It demonstrates the logical AND, OR, and NOT operations using np.logical_and()
, np.logical_or()
, and np.logical_not()
respectively.