NumPy: Using equal() and not_equal() functions (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, a cornerstone for numerical computing in Python, provides a comprehensive toolkit for working with arrays. Two of the fundamental functions that often go overlooked, yet are crucial for element-wise comparison of array elements, are equal() and not_equal(). These functions are indispensable when performing data comparison, filtering, or conditional operations on arrays. In this tutorial, we delve into using equal() and not_equal() through four progressively advanced examples.

Understanding equal() and not_equal()

The equal() function compares each element of an input array with a specified value or each element of another array, returning True where the comparison is true, and False otherwise. Conversely, not_equal() returns True for elements where the comparison is false. These functions support arrays of any shape and size, subject to broadcasting rules.

Example 1: Basic Comparison

The inaugural step in mastering these functions starts with a simple element-wise comparison. Let’s compare two arrays to understand equal() at an elementary level.

import numpy as np

# Create two arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([1, 2, 3, 2, 5])

# Use the equal() function
equal_result = np.equal(array1, array2)

# Output the result
print('Equal comparison: ', equal_result)

This code snippet returns an array of Boolean values, indicating the result of element-wise comparison:

Equal comparison: [ True True True False True]

Similarly, applying not_equal() to the same arrays yields the inverse:

# Use the not_equal() function
not_equal_result = np.not_equal(array1, array2)

# Output the result
print('Not equal comparison: ', not_equal_result)

Output:

Not equal comparison: [False False False True False]

Example 2: Comparison with Scalars

Next, we illustrate the utility of these functions in comparing each element in an array with a scalar value. This operation is common when filtering data or implementing conditional logic.

import numpy as np

# Create an array
array = np.array([1, 2, 3, 4, 5])

# Comparing each element to the scalar 3 using equal()
equal_scalar = np.equal(array, 3)

# Output
print('Comparison with scalar 3 (equal): ', equal_scalar)

Output:

Comparison with scalar 3 (equal): [False False True False False]

This time, let’s see how not_equal() works:

# Comparing each element to the scalar 3 using not_equal()
not_equal_scalar = np.not_equal(array, 3)

# Output
print('Comparison with scalar 3 (not_equal): ', not_equal_scalar)

Output:

Comparison with scalar 3 (not_equal): [ True True False True True]

Example 3: Working with Multidimensional Arrays

These functions are not limited to one-dimensional arrays. When working with multidimensional arrays, equal() and not_equal() can compare elements across arrays or within arrays and a scalar in a way that adheres to NumPy’s broadcasting rules. Let’s explore an example with a two-dimensional array.

import numpy as np

# Define two 2D arrays
array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[1, 0, 3], [4, 2, 6]])

# Use equal() on 2D arrays
equal_2d = np.equal(array1, array2)

# Display the result
print('2D Array comparison using equal():')
print(equal_2d)

Output:

2D Array comparison using equal():
[[ True False True]
[ True False True]]

Similarly, using not_equal() would highlight the differences:

# Use not_equal() on the same 2D arrays
not_equal_2d = np.not_equal(array1, array2)

# Display the result
print('2D Array comparison using not_equal():')
print(not_equal_2d)

Output:

2D Array comparison using not_equal():
[[False True False]
[False True False]]

Example 4: Advanced Application – Filtering Data

For our final example, we take a practical approach to utilizing equal() and not_equal() functions by filtering an array based on specific criteria. This method is particularly useful in data analysis or preprocessing.

import numpy as np

# Create an array of values
array = np.array([10, 20, 30, 40, 50])

# Filter the array to find elements equal to 30
equal_filter = np.equal(array, 30)
filtered_array = array[equal_filter]

# Display the filtered array
print('Filtered array (elements equal to 30): ', filtered_array)

Output:

Filtered array (elements equal to 30): [30]

Effectively, the Boolean array returned by equal() was used as a mask to filter out the desired elements. Similarly, not_equal() can be employed to exclude specific elements from an array.

Conclusion

Through these examples, we’ve seen how equal() and not_equal() provide a powerful means of comparing array elements, from basic to more sophisticated applications. Mastering these functions will enrich your NumPy proficiency, enabling you to perform an array of element-wise operations with ease.