Skip to main content

Filtering NumPy Array


In this tutorial you will learn:

  • What is Array Filtering?
  • Boolean Array based filtering

Array Filtering

Separating specific elements based on a specific condition and making another array from those elements is called filtering. Filtering can be carried out either by iterating complete array and checking each element against a specific condition or it can be carried out using by applying a Boolean array on the given array which we want to filter out.

Boolean Array Based Filtering

In NumPy, Boolean Array filtration is carried out using a Boolean index list, when the index is found “True”, that specific element will be added into the filtered array.

Lets take an example to further comprehend the process of Boolean array filtering. In this example you can observe that we have filtered any array by using a hard coded Boolean index array having Boolean elements “True” and “False”, so only those values will be filtered out which are having “True” at their respective index. In the third line of code we are declaring an array with 4 integer values, in the sixth line of code we are declaring the Boolean filter array and in tenth line we are applying the Boolean filter to the array which was declared in third line of the code. In results you can observe that only those elements are filtered out, which are having “True” at their respective index.

  1. import numpy as np
  2. #declaring an array to be filtered
  3. my_arr = np.array([1, 2, 3, 4])
  4. #printing the array which is to be filtered
  5. print('Array to be filtered:\n',my_arr)
  6. #declaring a boolean index list
  7. bin_arr = [False,True, False, True]
  8. #prinitng boolean index list
  9. print('Boolean index list which will be applied to filter the array:', bin_arr)
  10. #applying boolean index list to the array
  11. res = my_arr[bin_arr]
  12. #printing the filtered array
  13. print('Result after Array filtration: ', res)

Lets take another example, in this example we will be filtering an array of string using a boolean array. It must be kept in mind that the array being filtered and the Boolean index list must have the same number of elements, otherwise the compiler will give an error that the Boolean index did not match.

  1. import numpy as np
  2. #declaring an array to be filtered having data type string
  3. my_arr = np.array(['Source', 'Codester', 'Tutorials', 'Are','Fun','To','Learning'])
  4. #printing the String array which is to be filtered
  5. print('Array of DataType ''String'' to be filtered:\n',my_arr)
  6. #declaring a boolean index list
  7. bin_arr = [True,True, False, False,True,False,True]
  8. #prinitng boolean index list
  9. print('Boolean index list which will be applied to filter the array:', bin_arr)
  10. #applying boolean index list to the array
  11. res = my_arr[bin_arr]
  12. #printing the filtered array
  13. print('Result after Array filtration: ', res)

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.