Array Searching
Use the .where() method of numpy to return indices that match a certain condition.
import numpy as np
numpyarray = np.array([5, 6, 7, 8, 10])
bigger = np.where(numpyarray == 8)
print(bigger)
# result is:
(array([3]),)
because the 3rd index has a value equal to 8. Remember counting starts at 0.
What about if there are multiple matches?
import numpy as np
numpyarray = np.array([5, 6, 7, 8, 9, 10])
bigger = np.where(numpyarray > 7)
print(bigger)
# result is:
(array([3, 4, 5]),)
because the 3rd, 4th and 5th indices have values greater than 7.
What about if there are no matches?
import numpy as np
numpyarray = np.array([5, 6, 7, 8, 9, 10])
bigger = np.where(numpyarray < -4)
print(bigger)
# result is an empty array:
(array([], dtype=int64),)
Use .searchsorted() to find out where the proper position/index should be to insert a new element in such a way that the order is not destroyed. This method only works on an array that is already sorted to start with.
The default is to search for the index that is to the left of the first value that would destroy the order, imagining that the parameter value was to be inserted there.
import numpy as np
sortedarray = np.array([16, 27, 38, 49])
x = np.searchsorted(sortedarray, 33)
# you can use side = 'left' for clarity although it is the default.
print(x)
# result is:
2
because 33 belongs in the index spot 2, because if you put 33 into index spot 3, that would put the 38 to the left of it (33), out of order.
Although we still always move from left to right across the face of the target array, using the optional side = 'right' argument will instead find the index position to the right of where the new inserted value should be and still not destroy the order. Notice that this only produces a different result than if side was set to 'left' IF ONE OF THE ELEMENT VALUES IN THE TARGET LIST IS EQUAL TO THE NEW VALUE TO INSERT.
import numpy as np
sortedarray = np.array([16, 27, 38, 49])
x = np.searchsorted(sortedarray, 33, side = 'left')
print(x)
# result is:
2
Same result if side = 'right'.
import numpy as np
sortedarray = np.array([16, 27, 38, 49])
x = np.searchsorted(sortedarray, 33, side = 'right')
print(x)
# result is:
2
import numpy as np
sortedarray = np.array([16, 27, 33, 49])
x = np.searchsorted(sortedarray, 33, side = 'right')
print(x)
# result is:
3
because 49 at index 3 is to the right of the equaled 33 value that matched in the search. It is almost like that the new 33 was superimposed onto the current 33.
You can feed a list of new elements to .searchsorted() and it returns indices as if you had simultaneously dropped in all the new elements at once, meaning the target array is not in any intermediate state as each value could have dropped in one-by-one.
import numpy as np
array1 = np.array([1, 33, 25, 74])
x = np.searchsorted(array1, [22, 44, 66])
print(x)
# result is:
[1 3 3]
because 22 is in between 1 and 33 so it gets the index 1 position. Both 44 and 66 are in between 25 and 74. so each would get the same index 3 position, even though 66 is greater than 44. The method acts as if the target array is STATIC and values can "piled on top of each other" at the same index position.
With the side = 'right' optional argument into the .searchsorted() method, if the argument value is equal to or greater than the rightmost element of the target array (the largest value of the target array) then it will return an index representing what would be position to the right. In other words, the array could have potentially grown bigger by putting in your new element which is larger than any present element. Another way to think about this is if the array has grown or stretched out, and you are creating a new index all the way to the right that is not currently part of the target array.
import numpy as np
stretcharray = np.array([1, 33, 25, 74])
x = np.searchsorted(stretcharray, 88)
print(x)
# result is:
4
because 88 would need to appear in a new index position of 4 to keep the array ordered.
No comments:
Post a Comment