We have different methods in searching numerical values in NumPy.
In this article we have two main methods in searching:
- np.where()
- np.searchsorted()
np.where()
With this method, the compiler returns the indexes of elements in an input array where the given condition has a matched.
Syntax
Import numpy as np
numpy.where(condition[, x, y])
Condition: a given statement when if found true, the compiler yields x and yields y if false.
x,y: the values from which to choose x and y.
The following example explains how the np.where works
import numpy as np
arr = np.array(['a', 'c', 'b', 'e', 'g', 'b', 'b'])
x = np.where(arr == 'b')
print(x)
NumPy passes the command to the function which searches the given array. when the program is run, any letter below b and in the left of b is the letter to be indexed. The position of this letter is returned as the output.
OUTPUT
Let’s take another example
import numpy as np
arr = np.array([0, 1, 4, 6, 4, 3, 8, 3])
x = np.where(arr == 3)
print(x)
NumPy passes the command to the given function which searches the given array. when the program is run, any number which is greater than 3 and in the left of 3 is the value which will be indexed. And the output is the position of this number.
OUTPUT
np.searchsorted()
we use this function to find the indices in a sorted array, such that if we insert element before indexing, the order of the array would still be maintain. A binary search is always used to find the correct insertion space.
Syntax
import numpy as np
numpy.searchsorted(arr, num, side=’left’, sorter=None)
The following example will explain how this function works.
import numpy as np
arr = np.array([6, 7, 8, 9,8,8])
x = np.searchsorted(arr, 8)
print(x)
The command is passed on to the function which sorts out the given array. when the program is run/executed, the compiler follows the instructions of the function by searching for any number beside 8 from the left which is less than 8, and returns the position of this number as the output.
OUTPUT
As seen from the given syntax, this search method always starts from the left, but we can specify for the search to start from the right.
Example
import numpy as np
arr = np.array([6, 7, 8, 9,8,8])
x = np.searchsorted(arr, 8 , side='right')
print(x)
In this case, the search starts from the right, which in the case the compiler considers values which are greater than 8 and next to 8. The position of this values is returns as the result.
OUTPUT
We can also search for multiple arrays in an input of arrays. this array must contain the specified value.
Here’s an example to explain better
import numpy as np
arr = np.array([6, 7, 8, 9,8,8])
x = np.searchsorted(arr, 8 , side='right')
print(x)
The search takes place simultaneously at the same time. By default, the search starts from the left. The compiler searches for letters less than the specified letters and returns the position of the letters as the output.
OUTPUT
There are other functions to use when searching data in NumPy. They help in arranging data according to the given requirements. Hope the articles are very interesting. Have a nice day coding.
Comments
Post a Comment
Please do not enter any spam link in the comment box.