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
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
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
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.