In this article, we are going to learn how we can make use of the NumPy.where() function in some of the use cases where we need to filter some data based on a single condition to evaluate on some data set. so let us begin with our article on how to use Python np. where() with a single condition.
numpy.where() with single condition
We are going to take our first example with a single condition evaluation. In this example, we will evaluate if the single condition is true or false in the np.where() function and then execute the where function on arrays. let us jump to our example.
In this example, we are going to filter the array based on a single condition which is to check the number condition in the first array.
import numpy as np
# Create an array
arr1 = np.arange(0,10)
arr2 = np.arange(10,20)
print("The values of array 1 :\n", arr1)
print("The values of array 2 :\n", arr2)
result_arr = np.where((arr1 % 2 == 0),
arr1, arr2)
print("The filtered array is :\n", result_arr)
Output
The values of array 1 :
[0 1 2 3 4 5 6 7 8 9]
The values of array 2 :
[10 11 12 13 14 15 16 17 18 19]
The filtered array is :
[ 0 11 2 13 4 15 6 17 8 19]
In this second example, we are going to again use a single condition to filter out the array elements. Here we are checking the elements which are greater than 4 in the condition and based on this we are filtering the array of elements
import numpy as np
# Create an array
arr1 = np.arange(1,10)
arr2 = np.arange(4,13)
print("The values of array 1 :\n", arr1)
print("The values of array 2 :\n", arr2)
nparray= np.arange(1,10)
result_arr = np.where(nparray > 4,
arr1, arr2)
print("The filtered array is :\n", result_arr)
Output
The values of array 1 :
[1 2 3 4 5 6 7 8 9]
The values of array 2 :
[ 4 5 6 7 8 9 10 11 12]
The filtered array is :
[4 5 6 7 5 6 7 8 9]
Searching an Element in a Numpy Array by using Np.where()
Searching is one the most used concept in any array data structure. We can make use of the numpy.where() function to search an element in an array based on its value or a particular condition.
Here we have an example of the 1-D array and we are trying to search for an element in the array. We know the value of the element that we want to search and we are searching it by using numpy.where() function.
# Search an element in an 1-D array
import numpy as np
# Create an array
arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2,1])
result_index = np.where(arr1 == 6)
print("The resulted tuple index :\n", result_index)
print("The element found at index :\n", result_index[0],sep=',')
# Search an element that does not exist.
result_index = np.where(arr1 == 60)
print("\nThe resulted tuple index :\n", result_index)
print("\n The element found at index :\n", result_index[0],sep=',')
Output
The resulted tuple index :
(array([5, 9], dtype=int32),)
The element found at index :
,[5 9]
The resulted tuple index :
(array([], dtype=int32),)
The element found at index :
,[]
Search an element in an 2-D array
Here we have an example of the 2-D array and we are trying to search for an element in the array. We know the value of the element that we want to search and we are searching it by using the NumPy.where() function.
import numpy as np
# Create an array
arr1 = np.array([[1, 2, 3],
[4, 5, 6],
[6, 5, 4],
[3, 2, 1]])
print("The 2-D array is :\n", arr1)
result_arr = np.where(arr1 == 5)
print('Tuple of arrays returned : ', result_arr)
Output
The 2-D array is :
[[1 2 3]
[4 5 6]
[6 5 4]
[3 2 1]]
Tuple of arrays returned :
(array([1, 2], dtype=int32), array([1, 1], dtype=int32))
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 then you will need to take the first value from the first tuple and the second value from the second tuple. This means (1,1) is the first position of 5 and (2,1) is the second position of 5 in the 2-D array.
The row indexes will be part of result_arr[0] and column index will be part of result_arr[1].
We can use the zip() function of Python to make the index positions in a 2-D array.
import numpy as np
# Create an array
arr1 = np.array([[1, 2, 3],
[4, 5, 6],
[6, 5, 4],
[3, 2, 1]])
print("The 2-D array is :\n", arr1)
result_arr = np.where(arr1 == 5)
Indexpositions= list(zip(result_arr[0], result_arr[1]))
print(Indexpositions)
Output
The 2-D array is :
[[1 2 3]
[4 5 6]
[6 5 4]
[3 2 1]]
Index postion :
[(1, 1), (2, 1)]
Conclusion :
In this article, we learned, how we can use Python np.where() with the single condition in NumPy.where() function how we can search an element in 1-D and 2-D arrays.
We hope you learn many useful techniques in this article that will help you in your application code.