Skip to main content

# NumPy Conditional Filtering

In this tutorial you will learn:

• Condition Based Filtering
• Applying condition based filtering
• Conditional Statements Syntax

Condition Based Filtering

Filtering using a hard coded Boolean array is an impractical approach, likely problems that could be faced by programmers require an automated filtering. The modern automated filtering algorithms mostly being used for real time problems are based on Artificial Intelligence. In this tutorial we will use the basic conditional statements to filter out specific elements and learn the basic concept of conditional based filtering. The conditional statements will be coded using if, else if and else statements.

Applying Condition Based Filter

In this example I will produce a Boolean Filtering Array using a condition that if the number is less than 3, we will filter it out, hence only the numbers lesser than three will be included in our output array. Here you can observe that we are iterating through the complete array using a For Loop and on the basis of element and the condition we are appending ‘True’ or ‘False’ in the Boolean array. In the third line of code we are declaring the given array, in the seventh line of the code we are declaring empty Boolean array. In the eight line of code we are declaring a For loop and applying the condition in line nine. Finally in the fifteenth line we are applying the Boolean array to the given array and printing it in the same line.

`import numpy as np#declaring an array to be filteredmy_arr = np.array([1, 2, 3, 4,5,1,6,7,8,9,10,0,11,12,13,2,14,15,16])#printing the array which is to be filteredprint('Array to be filtered:\n',my_arr)#declaring a filter which will have true value at the index divisible by 4bool_array=[]for x in my_arr:  if ((x<3)):    bool_array.append(True)  else:    bool_array.append(False)#declaring a filter which will have true value at the index having value less than 3print('Indexes with values less less than 3:', bool_array)print('Filtered array with values less than 4:\n', my_arr[bool_array])`

Lets take a more comprehensive example, in this example we have made a filter which separates out the numbers divisible by 3 and 4. Here we will have four ‘if’ and ‘else’ statement while we will have two Boolean arrays each for the numbers divisible by 3 and 4.

`import numpy as np#declaring an array to be filteredmy_arr = np.array([1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16])#printing the array which is to be filteredprint('Array to be filtered:\n',my_arr)#declaring a filter which will have true value at the index divisible by 4bool_array_4=[]#declaring a filter which will have true value at the index divisible by 3bool_array_3=[]for x in my_arr:  if ((x % 4 == 0) & (x % 3 == 0)):   bool_array_4.append(True)   bool_array_3.append(True)  elif ((x % 4 == 0) & (x % 3 != 0)):   bool_array_4.append(True)   bool_array_3.append(False)  elif ((x % 4 != 0) & (x % 3 == 0)):   bool_array_4.append(False)   bool_array_3.append(True)  else:   bool_array_4.append(False)   bool_array_3.append(False)#printing boolean index list for divisible by 3print('Indexes divisible by 3:', bool_array_3)print('Filtered array divisible by 3:\n', my_arr[bool_array_3])#printing boolean index list for divisible by 4print('Indexes divisible by 4:', bool_array_4)print('Filtered array divisible by 4:\n', my_arr[bool_array_4])`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

## Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.