Comparing and Filtering NumPy array - GeeksforGeeks

07 Apr, 2021
In this article, we are going to see how to perform a comparison and filtering of the 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
# importing NumPy Moduleimport numpy as np   # Creating Arraya = np.array([1,2,3,4]) b = np.array([3,8,5,6])  # Comparing two arraysnp.greater(a, b)
Output:
array([False, False, False, False])
Example 2:
Import NumPy module.
Create array using numpy.array() method.
Now compare two arrays using less() method.
Python3
# Importing NumPy Moduleimport numpy as np  # Creating Array using NumPya = 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
# 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:
Import NumPy module.
Create array using numpy.array() method.
Now compare two arrays using not_equal() method.
Python3
# 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:
Import NumPy module.
Create array using numpy.array() method.
Now compare two arrays using >= operator.
Python3
# 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 bprint(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
# 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 bprint(a <= b)
Output:
[ True  True  True  True]
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])  # Taking a condition to filter the arrayfilter_ex = a < 16  # Creating new array using Condition.new_arr = np.array([filter_ex])  # Printing new Arrayprint(*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
# Importing  NumPy Moduleimport numpy as np  # Creating Arraya = np.array([1, 2, 3, 40, 50, 100,               45, 87, 98])  # Filtering Conditionfilter2 = a % 2 == 0even = np.array([filter2])print(*even)
Output:
[False  True False  True  True  True False False  True]
Picked
Python numpy-arrayManipulation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()