Open In App

Comparing and Filtering NumPy array

In this article, we are going to see how to perform a comparison and filtering of the NumPy array.

Comparing NumPy Array:

Let’s see the comparison operators that will be used in comparing NumPy Arrays –



Steps for NumPy Array Comparison:

Step 1: First install NumPy in your system or Environment. By using the following command.



pip install numpy(command prompt)
!pip install numpy(jupyter)

Step 2: Import NumPy module.

import numpy as np

Step 3: Create an array of elements using NumPy Array method.

np.array([elements])

Step 4: Now use comparison operators for comparing NumPy Array.

Example 1:




# importing NumPy Module
import numpy as np
 
# Creating Array
a = np.array([1,2,3,4])
b = np.array([3,8,5,6])
 
# Comparing two arrays
np.greater(a, b)

Output:

array([False, False, False, False])

Time complexity: O(n), where n is the length of the arrays a and b.
Auxiliary space: O(n), where n is the length of the arrays a and b, since we are creating two arrays of size n to store the inputs.

Example 2:




# Importing NumPy Module
import numpy as np
 
# Creating Array using NumPy
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
np.less(a, b)

Output:

array([ True,  True,  True,  True])

Example 3:




# Importing NumPy Module.
import numpy as np
 
# Create Arrays using np.array() Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
 
# Compare a and b array elements
# if the elements in a and b are equal
# it returns True else returns False.
np.equal(a, b)

Output:

array([ False,  False,  False, False])

Example 4:




# Importing NumPy Module.
import numpy as np
 
# Create Arrays using np.array() Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
 
# Compare a and b array elements if the
# elements in a and b are  not equal
# it returns True else returns False.
np.not_equal(a, b)

Output:

array([ True,  True,  True,  True])

Example 5:




# Importing NumPy Module.
import numpy as np
 
# Create Arrays using np.array()
# Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
 
# it returns if elements in a  are
# greater than a equal to b
print(a >= b)

Output:

[False False False False]

Example 6:




# Importing NumPy Module.
import numpy as np
 
 
# Create Arrays using np.array()
# Function.
a = np.array([1, 2, 3, 4])
b = np.array([3, 8, 5, 6])
 
# it returns if elements in a  are less
# than a equal to b
print(a <= b)

Output:

[ True  True  True  True]

Filtering NumPy Arrays:

Filtering means taking the elements which satisfy the condition given by us. For example, Even elements in an array, elements greater than 10 in an array, etc. 

Steps for Filtering NumPy Array’s:

Note: In Filtering and Comparison both give boolean values as an output.

Example 1:




import numpy as np
 
 
a = np.array([1, 2, 3, 40, 50, 100,
              45, 87, 98])
 
# Taking a condition to filter the array
filter_ex = a < 16
 
# Creating new array using Condition.
new_arr = np.array([filter_ex])
 
# Printing new Array
print(*new_arr)

Output:

[False False False  True  True  True  True  True  True]

Example 2:




# Importing  NumPy Module
import numpy as np
 
# Creating Array
a = np.array([1, 2, 3, 40, 50, 100,
              45, 87, 98])
 
# Filtering Condition
filter2 = a % 2 == 0
even = np.array([filter2])
print(*even)

Output:

[False  True False  True  True  True False False  True]

Article Tags :