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.

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

  1. import numpy as np
  2. #declaring an array to be filtered
  3. my_arr = np.array([1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16])
  4. #printing the array which is to be filtered
  5. print('Array to be filtered:\n',my_arr)
  6. #declaring a filter which will have true value at the index divisible by 4
  7. bool_array_4=[]
  8. #declaring a filter which will have true value at the index divisible by 3
  9. bool_array_3=[]
  10. for x in my_arr:
  11. if ((x % 4 == 0) & (x % 3 == 0)):
  12. bool_array_4.append(True)
  13. bool_array_3.append(True)
  14. elif ((x % 4 == 0) & (x % 3 != 0)):
  15. bool_array_4.append(True)
  16. bool_array_3.append(False)
  17. elif ((x % 4 != 0) & (x % 3 == 0)):
  18. bool_array_4.append(False)
  19. bool_array_3.append(True)
  20. else:
  21. bool_array_4.append(False)
  22. bool_array_3.append(False)
  23. #printing boolean index list for divisible by 3
  24. print('Indexes divisible by 3:', bool_array_3)
  25. print('Filtered array divisible by 3:\n', my_arr[bool_array_3])
  26. #printing boolean index list for divisible by 4
  27. print('Indexes divisible by 4:', bool_array_4)
  28. 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.