Related Articles
Comparing and Filtering NumPy array
• Last Updated : 07 Apr, 2021

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

 `# 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])`

Example 2:

• Import NumPy module.
• Create array using numpy.array() method.
• Now compare two arrays using less() method.

## Python3

 `# 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:

• 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 b ` `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

 `# 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:

• 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 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:

• 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 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]`

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :