codingstreets
Search
Close this search box.
python-numpy-filter

Introduction to Python NumPy Filter Array

In This Article, You Will Learn About Python Numpy Filter Array 

Python Numpy Filter – Before moving ahead, let’s know a little bit about Python NumPy Sorting Array

Filtering is the process of taking out elements from the original Array and create a new one.

A list of the Boolean indexes is available to filter a NumPy array.

If the value of the Index number belongs to True, then the element of that index number will filter; otherwise, the value of the index number False will not include an element to filter an array.

Example – Create an array from the elements on index Boolean value.

import numpy as np

Array = np.array([11, 12, 13, 14])
x = [True, False, False, Ture]
updated_array = Array[x]

print(updated_array)
Output -

[11, 14]

As shown above, it returned a filter array containing two elements i.e., [11, 14]. It is because the filter array included only those elements which are placed in place of True (Boolean value).

Creation of Filter Array

In the above example, the Array is filtered on Boolean values, i.e., True and False. But the primary purpose of taking Boolean value was to filter Array on a given condition.

Example – Creating a filtered array that returns multiples of 2.

import numpy as np

Array = np.array([1, 2, 3, 4, 5, 6, 7])

filter_ Array = []

for element in Array: 

# if the element is completely divisble by 2, take value True, otherwise False

     if element%2 == 0:
           filter_Array.append(True)
     else:
            filter_Array.append(False)

updated_Array = Array[filter_Array]
print(updated_Array)
Output -

[2, 4, 6, 8]

As a result, it returned a filtered array with the elements only divisble by 2.

Example – Creating a filtered array that returns odd numbers.

import numpy as np

Array = np.array([1, 2, 3, 4, 5, 6, 7])

filter_ Array = []

for element in Array: 

# if the element is not divisble by 2, take value True, otherwise False

     if element%2 != 0:
           filter_Array.append(True)
     else:
            filter_Array.append(False)

updated_Array = Array[filter_Array]
print(updated_Array)
Output -

[1 3 5 7]

As a result, it returned a filtered array with the elements not divisble by 2.

Filtering Array Directly from Array

As we can see, filtering the Array through Iterating each Array element becomes an easy understanding task in NumPy. Moreover, we have another substitute for it. Instead of Iterating each part of the Array, we can filter an array directly from the Array.

Example –  Creating a filtered array that returns multiples of 2.

import numpy as np

Array = np.array([1, 2, 3, 4, 5])
filter_Array = Array%2 == 0
updated_Array = Array[filter_Array]


print(updated_Array)
Output - 

[2, 4]

As a result, it returned a filtered array with the elements only divisble by 2.

Example – Creating a filtered array that returns odd numbers.

import numpy as np

Array = np.array([1, 2, 3, 4, 5])
filter_Array = Array%2 != 0
updated_Array = Array[filter_Array]

print(updated_Array)
python-numpy-filter

As a result, it returned a filtered array with the elements not divisble by 2.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on