| by Arround The Web | No comments

Numpy Filter

Fetching for elements or getting elements from some data is known as filtering. NumPy is the package that allows us to create arrays and store any type of data in the form of an array. When it comes to filtering in arrays while working with NumPy packages provided by python, it allows us to filter or get data from arrays using built-in functions provided by NumPy. A Boolean index list, a list of Booleans corresponding to array positions, can be utilized to filter arrays. If the element in the index of the array is true, it will be stored in the array unless the element is excluded from the array.

Let us suppose we have the data of students stored in the form of arrays and we want to filter out the failed students. We will simply filter the array and exclude the failed students and a new array of the passed student will be obtained.

Steps to filter a NumPy Array

Step 1: Importing NumPy module.

Step 2: Creating an array.

Step 3: Add filtering condition.

Step 4: Create a new filtered array.

Syntax:

There are multiple ways to filter arrays. It depends on the condition of the filter, like if we have only one condition or we have more than one condition.

Method 1: For One Condition We Will Follow the Following Syntax

array[array < condition]

In the syntax mentioned above, “array” is the name of the array from which we will filter the elements. And the condition will be the state on which the elements are filtered and the operator “<” is the mathematical sign that represents less than. It is efficient to use it when we only have one condition or statement.

Method 2: Using the “OR” Operator

array[(array < condition1) | (array > condition2)]

In this method, “array” is the name of the array from which we will filter values and the condition is passed to it. Operator “|” is used to representing the “OR” function which means from both conditions one should be true. It is useful when there are two conditions.

Method 3: Using the “AND” Operator.

array[(array < condition1) & (array > condition2)]

In the following syntax, “array” is the name of the array to be filtered. Whereas, the condition will be the state as discussed in the above syntax while the operator used “&” is the AND operator, which means both the condition must be true.

Method 4: Filtering by Listed Values

array[np.in1d(array, [List of values])]

In this method, we passed our defined array “np.in1d” which is used to compare two arrays whether the element of the array that is to be filtered is present in another array or not. And the array is passed to the np.in1d function that is to be filtered from the given array.

Example # 01:

Now, let us implement the above-discussed method in an example. Firstly, we will include our NumPy libraries provided by Python. Then, we will create an array named “my_array” that will be holding the values “2”, “3”, “1”, “9”, “3”, “5”, “6”, and “1”. Next, we will pass our filter code that is “my_array[(my_array < 5)]” to the print statement which means we are filtering the values that are less than “5”. In the next line, we created another array of name “array” that is responsible for having values “1”, “2”, “6”, “3”, “8”, “1” and “0”. To the print statement, we passed the condition that we will print the values that are greater than 5.

Lastly, we created another array which we named “arr”. It is holding the values “6”, “7”,”10”, “12”, and “14”. Now for this array, we will be printing the value that does not exist within the array to see what will happen if the condition does not match. To do so, we passed the condition which will filter the value that is equal to the value “5”.

import numpy as np

my_array= np.array([2, 3, 1, 9, 3, 5, 2, 6, 1])

print("values less than 5", my_array[(my_array < 5)])

array=np.array([1, 2, 6, 3, 8, 1, 0])

print("values greater than 5", array[(array > 5)])

arr=np.array([ 6, 7, 10, 12, 14])

print("values equal 5", arr[(arr == 5)])

After executing the code, we have the following output as a result, in which we have displayed the 3 outputs the first one is for the elements less than “5” in the second execution we printed the values greater than “5”. At the end, we printed the value that does not exist as we can see it does not display any error but displayed the empty array, which means the desired value does not exist in the given array.

Example # 02:

In this instance, we will use some of the methods in which we can use more than one condition to filter the arrays. To perform it, we will simply import the NumPy library and then create a one-dimensional array of size “9” having values “24”, “3”, “12”, “9”, “3”, “5”, “2”, “6”, and “7”. In the next line, we used a print statement to which we have passed an array that we have initialized with the name “my_array” with the condition as an argument. In this, we have passed the or condition which means from both of them, one condition must be true. If both of them are true, it will display the data for both of the conditions. In this condition, we want to print the values that are less than “5” and greater than “9”. In the next line, we used the AND operator to check what will happen if we use a condition to filter the array. In this condition, we displayed values that are greater than “5” and less than “9”.

Import numpy as np

my_array= np.array([24, 3, 12, 9, 3, 5, 2, 6, 7])

print(“values less than 5 or greater than 9, my_array[(my_array < 5) | (my_array > 9)])

print(“values greater than 5 and less than 9, my_array[(my_array > 5) & (my_array < 9)])

As shown in the snippet below, our result for the code above is displayed in which we filtered the array and got the following result. As we can see the values greater than 9 and less than 5 are displayed in the first output and the values between 5 and 9 are neglected. Whereas, in the next line, we have printed the values between “5” and “9” which are “6” and “7”. The other values of arrays are not displayed.

Conclusion

In this guide, we have briefly discussed the use of filter methods that are provided by the NumPy package. We have implemented multiple examples to elaborate for you on the best way to implement the filter methodologies provided by numpy.

Share Button

Source: linuxhint.com

Leave a Reply