NumPy Array Filtering

This article discusses NumPy array filtering methods and provides examples of how to implement them.



NumPy Array Filter

A Numpy array filter extracts some data from a given array and generates a new array from them.

By utilizing boolean index lists in NumPy, you can filter an array.

Array indexes are represented by true or false in boolean index lists.

The filtered array includes items whose values are True, whereas those whose values are False are not included.

Make an array utilizing the items at indexes 0,1,2,3,4,6 and 8:

Example: 

import numpy as npy fibonacci_arr = npy.array([0, 1, 1, 2, 3, 4, 5, 6, 8]) mrx = [True, True, True, True, True, False, True, False, True] ample = fibonacci_arr[mrx] print(ample)

NumPy Array Filtering

Why does the Numpy array filter above generate [0, 1, 1, 2, 3, 5, 8]?

In this case, indexes 0,1,2,3,4,6 and 8 of the filter array are included in the updated filter because it includes only values for which the filter array has the value True.

Utilize the items at indexes 0,2,3 and 5 to generate a true palindrome array:

Example: 

import numpy as npy palindrome_arr = npy.array(["101", "32526", "252", "25252", "53675", "89098"]) mrx = [True, False, True, True, False, True] ample = palindrome_arr[mrx] print(ample)
Numpy array filter code above displays [“101”, “252”, “25252”, “89098”], how come?
Because the updated filter only displays values for which the filter array has the value True, indexes 0, 2, 3, and 5 of the filter array are displayed.

Create a Filter Array

Since we are investigating Numpy array filters, the True and False values in the example above are hard-coded, but filter arrays are usually built dependent on conditions.

The following filter array will produce only values greater than 39:

Example: 

import numpy as npygreater_arr = npy.array([5,18,62,47,36,94,78])# Make a list that is void mrxfilter_arr = []# In even_arr, iterate over each number for mrx in greater_arr: # Set the value to True if the number is even, otherwise False: if mrx>39: mrxfilter_arr.append(True) else: mrxfilter_arr.append(False)ample_arr = greater_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Display only words whose length is equal to five with a filter array:

Example: 

import numpy as npywords_arr = npy.array(["Adapt","Baker","Laptop","Sneakers","Cable","Firm","Dagon","Eagle","Wire","Ideal"])# Make a list that is void mrxfilter_arr = []# In even_arr, iterate over each number for mrx in words_arr: # Set the value to True if the number is even, otherwise False: if len(mrx) == 5: mrxfilter_arr.append(True) else: mrxfilter_arr.append(False)ample_arr = words_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Make a filter array that will display only even numbers:

Example: 

import numpy as npyeven_arr = npy.array([0, 1, 2, 3, 4 ,6, 8, 9, 10, 12])# Make a list that is void mrxfilter_arr = []# In even_arr, iterate over each number for mrx in even_arr: # Set the value to True if the number is even, otherwise False: if mrx%2 == 0: mrxfilter_arr.append(True) else: mrxfilter_arr.append(False)ample_arr = even_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Show only odd numbers with a filter array:

Example: 

import numpy as npyodd_arr = npy.array([0, 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12, 13])# Make a list that is void mrxfilter_arr = []# In even_arr, iterate over each number for mrx in odd_arr: # Set the value to True if the number is even, otherwise False: if mrx%2 != 0: mrxfilter_arr.append(True) else: mrxfilter_arr.append(False)ample_arr = odd_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Array-Based Filter Creation

As it comes to Numpy array filters, NumPy offers a convenient approach to interacting with the above example.

If we replace the array in place of an iterable variable in our condition, it will work as it should.

Only values larger than 39 will be obtained from the following filter array:

Example: 

import numpy as npygreater_arr = npy.array([5,18,62,47,36,94,78])mrxfilter_arr = greater_arr > 39ample_arr = greater_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Print the numbers that are multiples of five into a filter array:

Example: 

import numpy as npynumber_arr = npy.array([7, 10, 12,14,15, 20, 23, 25])mrxfilter_arr = number_arr%5 == 0ample_arr = number_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Make a filter array that will output a list of even numbers:

Example: 

import numpy as npyeven_arr = npy.array([0, 1, 2, 3, 4 ,6, 8, 9, 10, 12])mrxfilter_arr = even_arr%2 == 0ample_arr = even_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)

Show a list of odd numbers with a filter array:

Example: 

import numpy as npyodd_arr = npy.array([0, 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12, 13])mrxfilter_arr = odd_arr%2 != 0ample_arr = odd_arr[mrxfilter_arr]print(mrxfilter_arr) print(ample_arr)
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *