# 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.

`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 a filter in which all numbers are less than 7res =my_arr[my_arr &lt; 7]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.

`import numpy as np#declaring an array with DataType float to be filteredmy_arr = np.array([8.2,1.1,2.3,4.4,7.7])#printing the array which is to be filteredprint('\n Array to be filtered:\n',my_arr)#declaring a a filter in which all numbers are less than 7.5res =my_arr[my_arr &lt; 7.5]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.

`import numpy as np#declaring an arraymy_arr = np.array([8,1,19,2,10,14,12,20,11,13])#printing the array which is to be filteredprint('\n Array to be filtered:\n',my_arr)#Creating a filter to filter out odd numbers onlyfilter_arr = my_arr%2==1#applying the filter on the given arrayres =my_arr[filter_arr]print('The result after filtering the odd numbers in array:', 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.