How to find the closest value that is larger in a numpy array?

I would like to get the index of the closest value in a numpy array, which is larger than my search value. Example: findNearestAbove(np.array([0.,1.,1.4,2.]), 1.5) should return 3 (index 2.).

I know that I can get the closest index using np.abs(a-value).argmin() , and I found that min(a[np.where(a-value >= 0.)[0]]) returns the required array value. Therefore, np.where(a == min(a[np.where(a-value >= 0.)[0]]))[0] will probably give me the desired index. However, this seems rather confusing, and I'm afraid that it might break in the case of multidimensional arrays. Any suggestions for improving this?

+4
source share
4 answers

Here is one way (I assume that the closest you understand the meaning is out of place)

 import numpy as np def find_nearest_above(my_array, target): diff = my_array - target mask = np.ma.less_equal(diff, 0) # We need to mask the negative differences and zero # since we are looking for values above if np.all(mask): return None # returns None if target is greater than any value masked_diff = np.ma.masked_array(diff, mask) return masked_diff.argmin() 

Result:

 >>> find_nearest_above(np.array([0.,1.,1.4,2.]), 1.5) 3 >>> find_nearest_above(np.array([0.,1.,1.4,-2.]), -1.5) 0 >>> find_nearest_above(np.array([0., 1, 1.4, 2]), 3) >>> 
+5
source

I believe you can use np.searchsorted for this:

 In [15]: np.searchsorted(a,[1.5,],side='right')[0] Out[15]: 3 

Assuming a is in ascending order.

This method will also not work for multidimensional arrays, but I'm not sure exactly how this use case will work in terms of the expected result. If you could give an example of what you imagine, I could adapt this for this purpose.

Note: for this purpose you can also use np.digitize , although it performs a linear rather than binary search, so for a specific input dimensions, it can be much slower than searchsorted and requires a be monotonous:

 In [25]: np.digitize([1.5,], a, right=True)[0] Out[25]: 3 
+6
source

Here is a solution that worked very well for me when searching for the value and index of the closest, but larger than the numbers in the array (without promises in terms of speed, etc.):

 def findNearestGreaterThan(searchVal, inputData): diff = inputData - searchVal diff[diff<0] = np.inf idx = diff.argmin() return idx, inputData[idx] 

It adapts easily to the closest, but smaller:

 def findNearestLessThan(searchVal, inputData): diff = inputData - searchVal diff[diff>0] = -np.inf idx = diff.argmax() return idx, inputData[idx] 
0
source

Here is the right way to do this:

 >>> def argfind(array, predicate): ... for i in xrange(array.shape[0]): ... if predicate(array[i]): ... return i ... return False ... >>> def find_nearest_above(array, value): ... return argfind(array, lambda x: x > value) ... >>> find_nearest_above(np.array([0.,1.,1.4,2.]), 1.5) > 3 

The point here is that if a suitable value exists, you will get an answer when that value is executed. Other methods (including your own suggested in the question) will check the entire array, which is a waste of time.

-3
source

Source: https://habr.com/ru/post/1486326/


All Articles