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 –
- Greater than (>) Or numpy.greater().
- Less Than (<) numpy.less().
- Equal(==) or numpy.equal()
- Not Equal(!=) or numpy.not_equal().
- Greater than and equal to(>=).
- Less than Equal to(<=).
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:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using greater() method.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 4 ])
b = np.array([ 3 , 8 , 5 , 6 ])
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:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using less() method.
Python3
import numpy as np
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:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using equal() method.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 4 ])
b = np.array([ 3 , 8 , 5 , 6 ])
np.equal(a, b)
|
Output:
array([ False, False, False, False])
Example 4:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using not_equal() method.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 4 ])
b = np.array([ 3 , 8 , 5 , 6 ])
np.not_equal(a, b)
|
Output:
array([ True, True, True, True])
Example 5:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using >= operator.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 4 ])
b = np.array([ 3 , 8 , 5 , 6 ])
print (a > = b)
|
Output:
[False False False False]
Example 6:
- Import NumPy module.
- Create array using numpy.array() method.
- Now compare two arrays using <= operator.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 4 ])
b = np.array([ 3 , 8 , 5 , 6 ])
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:
- Import NumPy module.
- Create arrays using np.array() function.
- Write any condition for filtering the array.
- Create a new array with that filtering function.
Note: In Filtering and Comparison both give boolean values as an output.
Example 1:
- Import NumPy module.
- Create array using numpy.array() method.
- Now take a condition for filtering array.
- Now create a new array that satisfies the condition.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 40 , 50 , 100 ,
45 , 87 , 98 ])
filter_ex = a < 16
new_arr = np.array([filter_ex])
print ( * new_arr)
|
Output:
[False False False True True True True True True]
Example 2:
- Import NumPy module.
- Create array using numpy.array() method.
- Now take a condition for filtering array.
- Now create a new array that satisfies the condition.
Python3
import numpy as np
a = np.array([ 1 , 2 , 3 , 40 , 50 , 100 ,
45 , 87 , 98 ])
filter2 = a % 2 = = 0
even = np.array([filter2])
print ( * even)
|
Output:
[False True False True True True False False True]