In this post,we will learn how to Filter 2D NumPy array based on condition by using the different NumPy libaray function that includes numpy.any(),numpy.all(),nump.where(),
1.NumPy.any() to filter 2D NumPy array based on condition
The np.any() method is used to validate a condition whether any element of the numpy array is returning True. In the below example we are using numpy.any() to filter row that has any element is 5 or 12.So as per the given test the row 1st,3rd, and 4th rows is filtered
- We have created an array and reshape it into size of 5 rows and 5 columns.
- The numpy.any() method to filter any rows that satisfy the condition where element 5 or 12.
- Finally printing the filter array
import numpy as np
myarr = np.arange(25).reshape((5, 5))
print(myarr)
filterArr = myarr[np.any((myarr == 5) | (myarr == 12), axis=0)]
print('\nFiltter array :\n',filterArr)
Output
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Filtter array :
[[ 0 1 2 3 4]
[10 11 12 13 14]]
4. NumPy.all() to filter 2D NumPy array
The numpy.all() function will check if all elements within a given axis pass the condition or return True. It checks if all the element is equal to TRUE.
- We have created a numpy array using of size(25) and diestrubuted into 5 rows and 5 columns.
- The np.all() function return an numpy array of elements that satisfy the condition.
- Finally printing the result array using the print() function
import numpy as np
myarr = np.arange(25).reshape((5, 5))
print(myarr)
print('result array:',myarr[1, ~np.all(myarr<=5, axis=0)])
Output
Original Array:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Result array:
[5 6 7 8 9]
3. np.in1d() to Filter 2D NumPy array based on condition
The np.in1d() method is used to check each element of the 1D array also present in the given array.In this below example we are checking if the item_to_fltr element is present in arr.
- We have created an array by using the np.asarray() method
- Used the np.in1d() method to filter the row based on condition.
import numpy as np
arr = np.asarray([[1, 12], [2, 4], [3, 5],
[4, 6], [5, 8]])
item_to_fltr = np.asarray([2, 5])
filterarr = arr[np.in1d(arr[:, 1], item_to_fltr)]
print('filter array:\n',filterarr)
Output
filter array:
[[3 5]]
4. np.where()to Filter 2D NumPy array based on condition
In this below example, we are using np.where() to filter the numpy array based on mutiple conditions.If you see the output, the output is the tuple of arrays.
This contains two arrays. These two arrays contain the index of each dimension. The first array contains the row index and the second array contains the column index.so if you want to know the element position, you will need to take the first value from the first tuple and the second value from the second tuple.
The row indexes will be part of result_arr1[0] and the column index will be part of result_arr1[1]. We can use the zip() function of Python to make the index positions in a 2-D array
import numpy as np
arr1 = np.array([[6, 13, 22, 7, 12],
[7, 11, 16, 32, 9]])
result_arr1 = np.where(((arr1 % 2 == 0) | (arr1 % 2 == 1)))
print('Using Or operator:\n',result_arr1)
Indexpositions1= list(zip(result_arr1[0], result_arr1[1]))
print(Indexpositions1)
print("\nThe filtered index positions with OR Condition:\n", Indexpositions1)
Output
Using Or operator:
(array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=int32), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=int32))
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
The filtered index positions with OR Condition:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]