Table of Contents
ToggleElement-wise comparisons in NumPy allow you to compare each element of one array with the corresponding element of another array or a scalar value.
The comparison is performed across the entire array, and the result is a new array of the same shape where each element is a Boolean (True or False) indicating the outcome of the comparison.
NumPy supports several basic comparison operations that can be performed element-wise. These include −
In the following example, each comparison operation is performed between corresponding elements of array1 and array2 −
# Open Compiler
import numpy as np
# Creating two arrays for comparison
array1 = np.array([10, 20, 30, 40, 50])
array2 = np.array([15, 20, 25, 40, 55])
# Performing element-wise comparisons
equality = array1 == array2
inequality = array1 != array2
greater_than = array1 > array2
less_than = array1 < array2
greater_equal = array1 >= array2
less_equal = array1 <= array2
# Displaying the results
print("Equality:", equality)
print("Inequality:", inequality)
print("Greater than:", greater_than)
print("Less than:", less_than)
print("Greater than or equal to:", greater_equal)
print("Less than or equal to:", less_equal)
Output:
The result is a Boolean array indicating the outcome of each comparison as shown below −
Equality: [False True False True False]
Inequality: [ True False True False True]
Greater than: [False False True False False]
Less than: [ True False False False True]
Greater than or equal to: [False True True True False]
Less than or equal to: [ True True False True True]
You can also compare an entire array with a single scalar value. The scalar value is compared to each element of the array, and the result is a Boolean array of the same shape.
In this example, each element of array1 is compared to "30", and the result indicates whether each element is greater than "30" −
# Open Compiler
import numpy as np
# Creating two arrays for comparison
array1 = np.array([10, 20, 30, 40, 50])
# Comparing array elements with a scalar value
scalar_value = 30
comparison_result = array1 > scalar_value
print("Array elements greater than 30:", comparison_result)
Output:
This will produce the following result −
Array elements greater than 30: [False False False True True]
Chaining multiple comparisons in NumPy involves using logical operators to combine several comparison operations. For instance, you might want to check if the elements of an array fall within a specific range or if they satisfy multiple criteria.
The operations are evaluated in sequence, and the result is a Boolean array where each element indicates whether the combined conditions are met.
In NumPy, you can chain comparisons using logical operators like & (and), | (or), and ~ (not). When chaining comparisons, ensure that each comparison operation is enclosed in parentheses to maintain the correct order of operations. Here is the general syntax for chaining comparisons −
(condition1) & (condition2) & ... & (conditionN)
In the example below, we are checking if the elements of an array are within a specific range and satisfy additional conditions or not −
# Open Compiler
import numpy as np
# Creating an array
array = np.array([5, 10, 15, 20, 25, 30])
# Chaining multiple comparisons
result = (array > 10) & (array < 25) & (array % 5 == 0)
# Displaying the results
print("Array:", array)
print("Result of Chained Comparisons:", result)
Output:
Following is the output of the above code −
Array: [ 5 10 15 20 25 30]
Result of Chained Comparisons: [False False True True False False]
Here, the comparison checks if each element of the array is between "3" and "7", inclusive −
# Open Compiler
import numpy as np
# Creating an array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Chaining comparisons with scalar values
result = (array >= 3) & (array <= 7)
# Displaying the results
print("Array:", array)
print("Result of Chained Comparisons with Scalar:", result)
Output:
The output obtained is as shown below −
Array: [1 2 3 4 5 6 7 8 9]
Result of Chained Comparisons with Scalar: [False False True True True True True False False]
The np.where() function uses the results of element-wise comparisons to selectively choose elements from one of two arrays (or values). This is particularly useful for filtering or replacing elements based on a condition.
In this example, elements of array1 greater than "25" are kept, while all others are replaced with "0" −
# Open Compiler
import numpy as np
# Creating an array
array1 = np.array([10, 20, 30, 40, 50])
# Using np.where to replace elements based on a condition
replaced_array = np.where(array1 > 25, array1, 0)
print("Replaced array:", replaced_array)
Output:
After executing the above code, we get the following output −
Replaced array: [ 0 0 30 40 50]
Element-wise comparisons can be used in conjunction with functions like np.maximum() and np.minimum() to find the maximum or minimum values between two arrays.
In this example, we use np.maximum() function and np.minimum() function to compare elements of array1 and array2, returning arrays of the maximum and minimum values respectively −
# Open Compiler
import numpy as np
# Creating an array
array1 = np.array([10, 20, 30, 40, 50])
array2 = np.array([15, 20, 25, 40, 55])
# Finding maximum and minimum values between two arrays
max_array = np.maximum(array1, array2)
min_array = np.minimum(array1, array2)
print("Maximum values:", max_array)
print("Minimum values:", min_array)
Output:
The result produced is as follows −
Maximum values: [15 20 30 40 55]
Minimum values: [10 20 25 40 50]
Key Takeaway: Master element-wise comparisons in NumPy with Vista Academy!
