Element-wise Comparisons in NumPy: Using the '<' Operator


Element-wise Comparison

When you use the < operator on NumPy arrays, it performs an element-wise comparison. This means it compares the corresponding elements at the same position in both arrays. For each pair of elements, it returns True if the element in the first array is less than the element in the second array. Otherwise, it returns False.

Example

import numpy as np

# Create two NumPy arrays
arr1 = np.array([1, 3, 5])
arr2 = np.array([2, 4, 1])

# Less-than comparison using the '<' operator
comparison = arr1 < arr2

# Print the results
print(comparison)  # Output: [ True  True False]

In this example:

  • The output [True, True, False] shows that:
    • 1 in arr1 is less than 2 in arr2, so True.
    • 3 in arr1 is less than 4 in arr2, so True.
    • 5 in arr1 is not less than 1 in arr2, so False.
  • The comparison array holds the results of the element-wise comparison.
  • arr2 has elements [2, 4, 1].
  • arr1 has elements [1, 3, 5].


Example 1: Scalar comparison

This example compares an ndarray with a scalar value.

import numpy as np

arr = np.array([5, 2, 8])
scalar = 3

# Compare each element in 'arr' with the scalar '3'
comparison = arr < scalar

# Print the results
print(comparison)  # Output: [False  True False]

Example 2: Broadcasting with different shapes

This example demonstrates broadcasting with arrays of different shapes.

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([5])

# Compare 'arr1' with 'arr2' (broadcasting happens)
comparison = arr1 < arr2

# Print the results
print(comparison)  # Output: [ True  True  True]

In this case, arr2 has a single element (5), which gets broadcast to match the shape of arr1 during the comparison.

Example 3: Multi-dimensional arrays

This example compares elements in multi-dimensional arrays.

import numpy as np

arr1 = np.array([[1, 4], [3, 2]])
arr2 = np.array([[2, 1], [5, 4]])

# Element-wise comparison
comparison = arr1 < arr2

# Print the results
print(comparison)  # Output: [[ True False]  [ True False]]

The comparison happens between corresponding elements at the same position in both arrays.



Using NumPy functions

  • np.less(arr1, arr2): This function performs the same element-wise less-than comparison as arr1 < arr2. It's a more explicit way and returns a boolean NumPy array with the comparison results.
import numpy as np

arr1 = np.array([1, 3, 5])
arr2 = np.array([2, 4, 1])

comparison = np.less(arr1, arr2)
print(comparison)  # Output: [ True  True False]
  • Other comparison functions like np.greater(arr1, arr2) (greater than), np.equal(arr1, arr2) (equal to), etc. are available for different comparisons.

List comprehension

While less efficient for larger arrays, you can use list comprehension to achieve element-wise comparisons.

arr1 = np.array([1, 3, 5])
arr2 = np.array([2, 4, 1])

comparison = [a < b for a, b in zip(arr1, arr2)]
print(comparison)  # Output: [True, True, False]

This approach iterates through corresponding elements in both arrays using zip and performs the comparison within the list comprehension.

  • For the most efficient and concise method, using the ndarray.__lt__() method with the < operator remains the recommended approach.
  • If you're working with smaller arrays and need more control over the comparison logic, list comprehension might be an option.
  • For readability and leveraging NumPy's vectorized operations, np.less or similar functions are often preferred.