NumPy Direct Filtering

In this tutorial you will learn:

  • NumPy Direct Filtering
  • How to carry out direct filtering
  • Python Syntax

Direct Filtering

Filtering numerical arrays is a very common task in NumPy, inorder to save time and ensure max code efficiency while filtering, NumPy library provides the feature of direct filtering. Using Direct Filtering we can filter out an array without using the conditional statements. For direct filtering we create a filter directly from the array, the usage of direct filtering makes the code more compact and simple.

Carrying out Direct Filtering

In order to carry out direct filtering we require a filter which we can directly apply to the given array in the same line of code or we can define the filter separately and apply the filter in separate line.

Lets take an example, in this example you can see that we have put a condition that if the element is greater than or equal to 7, the index will be filtered out and not placed in the new array. In the third line we have declared an array, in the seventh line of code we have developed the filter and applied the filter on the array.

  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 a filter in which all numbers are less than 7
  7. res =my_arr[my_arr < 7]
  8. print('The result after filtering the array:', res)

Lets take another example, in this example we will be applying direct filtering on an array having Data Type Float. Here you can have a clear idea that the direct filtering is only applicable on the numerical arrays only because filtering any alphabetical element cannot be carried out through operators.

  1. import numpy as np
  2. #declaring an array with DataType float to be filtered
  3. my_arr = np.array([8.2,1.1,2.3,4.4,7.7])
  4. #printing the array which is to be filtered
  5. print('\n Array to be filtered:\n',my_arr)
  6. #declaring a a filter in which all numbers are less than 7.5
  7. res =my_arr[my_arr < 7.5]
  8. print('The result after filtering the array:', res)

Lets take another example, in this example first we will be creating a filter separately. The filter will only filter out the odd numbers out whenever applied on the given array. Here we are creating a filter in seventh line of the code and applying it to the given array in the ninth line of the code. In results you can observe that the filtered numbers are displayed exactly in the sequence as they were initially declared in third line of the code.

  1. import numpy as np
  2. #declaring an array
  3. my_arr = np.array([8,1,19,2,10,14,12,20,11,13])
  4. #printing the array which is to be filtered
  5. print('\n Array to be filtered:\n',my_arr)
  6. #Creating a filter to filter out odd numbers only
  7. filter_arr = my_arr%2==1
  8. #applying the filter on the given array
  9. res =my_arr[filter_arr]
  10. print('The result after filtering the odd numbers in array:', res)

Add new comment