Comparing and Filtering NumPy array


The NumPy library has a wide range of tools which perform the comparison and filtering of the arrays. The comparison of arrays will be done element by element in row wise and column wise based on the dimension of the array. When we want to retrieve a particular array element, then we can apply the filtering.

NumPy is abbreviated as Numerical python, which is used to perform mathematical and scientific calculations on multidimensional arrays and matrices. There are different functions and methods available in NumPy to perform the filtering and comparison of the arrays.

Comparing NumPy Arrays

Following are the methods available in NumPy library to perform the comparison operations on the arrays.

  • equal()

  • greater()

  • array_equal()

  • all_close()

Using equal() Method

The equal() method in NumPy library compares the two NumPy arrays element-wise and returns a Boolean output for each comparison performed. To elaborate, when one element from the first array is compared with its corresponding element in the second array, ‘True’ value is returned if they are equal and ‘False’, otherwise. This process is repeated for all the elements in both these arrays.

All these Boolean values obtained are stored in another array and displayed as the output. If the output array contains all ‘True’ values, then these arrays are said to be identical and ‘False’, otherwise.

Example

Let us see a simple example comparing two NumPy arrays element by element, using the equal() method –

import numpy as np
a = np.array([1,2,3])
b = np.array([34,2,10])
out = np.equal(a,b)
print("The Boolean output of the equal function:",out)

Output

The following is the output of the equal function applied on the arrays and we can observe the Boolean output.

The Boolean output of the equal function: [False True False]

Using greater() Method

The greater() method is used to compare any two NumPy arrays element-wise. This method returns True if the element in first array is greater than its corresponding element in the second array; and False, otherwise.

Example

In this example, we are trying to compare elements of two NumPy arrays using the greater() method. The return values after each comparison are stored in another array.

import numpy as np
a = np.array([1,2,3])
b = np.array([34,2,10])
out = np.greater(a,b)
print("The Boolean output of the greater function:",out)

Output

The following is the output of the greater function when applied on the two arrays.

The Boolean output of the greater function: [False False False]

Using array_equal() Method

The array_equal() method is used to compare two NumPy arrays element-wise to check whether both the arrays are equal or not. If both arrays are equal, the return value is True and if not, the return value is False.

Note − Both arrays are said to be equal only if all the elements in the arrays are equal.

Example

When we pass the two arrays of same shape and size to the array_equal() function then the output will be in Boolean format.

import numpy as np
a = np.array([1,2,3])
b = np.array([34,2,10])
out = np.array_equal(a,b)
print("The Boolean output of the array_equal function:",out)

Output

The following is the output of the array_equal() function.

The Boolean output of the array_equal function: False

Using allclose() Method

The allclose() method compares two NumPy arrays element-wise and checks which element is close to each other.

Example

In this example if we pass the two input arrays to the allclose() function then it will gives true when the elements are close otherwise gives false.

import numpy as np
a = np.array([1,2,3])
b = np.array([3,4,2])
out = np.allclose(a,b)
print("The Boolean output of the allclose function:",out)

Output

The following is the output of the allclose() function and returns the Boolean output.

The Boolean output of the allclose function: False

Filtering NumPy Arrays

The following are the functions to perform the filtering operations on the arrays.

  • Boolean indexing

  • where() method

  • extract() method

  • delete() method

Boolean indexing

Boolean Indexing allows us to select elements from an array based on a Boolean condition, i.e. only those elements that satisfy the Boolean conditions are extracted from the array. This condition, which needs to be satisfied with the array elements, is also known as Boolean mask.

Example

In this example, we are trying to retrieve the filtered elements of an array using Boolean indexing. To filter elements, we will first create a Boolean mask, with a condition a > 2, then the elements satisfying this condition are extracted.

import numpy as np
a = np.array([34,2,10])
mask = a > 2
filtered_array = a[mask]
print("The output of the Boolean indexing:",filtered_array)

Output

The following is the output of the Boolean indexing applied on the array.

The output of the Boolean indexing: [34 10]

Using where() Method

The where() method is used to filter the elements from an array, based on a condition given by the user. It returns the indices of the array elements that satisfy the given condition.

Example

Following example shows how to filter elements in a NumPy array using the where() method. Here, we are passing a condition (a>=2) as an argument to the where() method and only those values that satisfy this condition are displayed in the output array.

import numpy as np
a = np.array([34,2,10])
filtered_array = np.where(a <=2)
print("The output of the where function :",filtered_array)

Output

The following is the output of the where function applied on the input arrays.

The output of the where function : (array([1]),)

Using extract() Method

The extract() method extracts all the elements that satisfy the given condition, as its name implies.

Example

Here, we are passing an array along with the condition as arguments to the extract() function. The elements in this array that satisfy the gven condition are expected to be extracted.

import numpy as np
a = np.array([34,2,10])
filtered_array = np.extract(a <=2, a)
print("The output of the extract function :",filtered_array)

Output

The below is the output of the extract function applied on the given input array.

The output of the extract function : [2]

Using delete() Method

The delete() method is used to delete elements from a NumPy array, based on a condition specified by the user.

The return value will be the array after deleting the elements in the given array depending on the condition. The condition argument we will use in this method is obtained from the where() method.

Example

Here in this example, when we pass the input array along with the condition (obtained from where() method to the delete() method, the array elements that satisfy the given condition are deleted.

import numpy as np
a = np.array([34,2,10])
filtered_array = np.delete(a, np.where(a == 2))
print("The output of the delete function :",filtered_array)

Output

The following is the output of the delete() function.

The output of the delete function : [34 10]

Updated on: 07-Aug-2023

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements