Find nearest value in numpy array

后端 未结 16 1989
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 10:18

Is there a numpy-thonic way, e.g. function, to find the nearest value in an array?

Example:

np.find_nearest( array, value )
16条回答
  •  礼貌的吻别
    2020-11-22 10:40

    Summary of answer: If one has a sorted array then the bisection code (given below) performs the fastest. ~100-1000 times faster for large arrays, and ~2-100 times faster for small arrays. It does not require numpy either. If you have an unsorted array then if array is large, one should consider first using an O(n logn) sort and then bisection, and if array is small then method 2 seems the fastest.

    First you should clarify what you mean by nearest value. Often one wants the interval in an abscissa, e.g. array=[0,0.7,2.1], value=1.95, answer would be idx=1. This is the case that I suspect you need (otherwise the following can be modified very easily with a followup conditional statement once you find the interval). I will note that the optimal way to perform this is with bisection (which I will provide first - note it does not require numpy at all and is faster than using numpy functions because they perform redundant operations). Then I will provide a timing comparison against the others presented here by other users.

    Bisection:

    def bisection(array,value):
        '''Given an ``array`` , and given a ``value`` , returns an index j such that ``value`` is between array[j]
        and array[j+1]. ``array`` must be monotonic increasing. j=-1 or j=len(array) is returned
        to indicate that ``value`` is out of range below and above respectively.'''
        n = len(array)
        if (value < array[0]):
            return -1
        elif (value > array[n-1]):
            return n
        jl = 0# Initialize lower
        ju = n-1# and upper limits.
        while (ju-jl > 1):# If we are not yet done,
            jm=(ju+jl) >> 1# compute a midpoint with a bitshift
            if (value >= array[jm]):
                jl=jm# and replace either the lower limit
            else:
                ju=jm# or the upper limit, as appropriate.
            # Repeat until the test condition is satisfied.
        if (value == array[0]):# edge cases at bottom
            return 0
        elif (value == array[n-1]):# and top
            return n-1
        else:
            return jl
    

    Now I'll define the code from the other answers, they each return an index:

    import math
    import numpy as np
    
    def find_nearest1(array,value):
        idx,val = min(enumerate(array), key=lambda x: abs(x[1]-value))
        return idx
    
    def find_nearest2(array, values):
        indices = np.abs(np.subtract.outer(array, values)).argmin(0)
        return indices
    
    def find_nearest3(array, values):
        values = np.atleast_1d(values)
        indices = np.abs(np.int64(np.subtract.outer(array, values))).argmin(0)
        out = array[indices]
        return indices
    
    def find_nearest4(array,value):
        idx = (np.abs(array-value)).argmin()
        return idx
    
    
    def find_nearest5(array, value):
        idx_sorted = np.argsort(array)
        sorted_array = np.array(array[idx_sorted])
        idx = np.searchsorted(sorted_array, value, side="left")
        if idx >= len(array):
            idx_nearest = idx_sorted[len(array)-1]
        elif idx == 0:
            idx_nearest = idx_sorted[0]
        else:
            if abs(value - sorted_array[idx-1]) < abs(value - sorted_array[idx]):
                idx_nearest = idx_sorted[idx-1]
            else:
                idx_nearest = idx_sorted[idx]
        return idx_nearest
    
    def find_nearest6(array,value):
        xi = np.argmin(np.abs(np.ceil(array[None].T - value)),axis=0)
        return xi
    

    Now I'll time the codes: Note methods 1,2,4,5 don't correctly give the interval. Methods 1,2,4 round to nearest point in array (e.g. >=1.5 -> 2), and method 5 always rounds up (e.g. 1.45 -> 2). Only methods 3, and 6, and of course bisection give the interval properly.

    array = np.arange(100000)
    val = array[50000]+0.55
    print( bisection(array,val))
    %timeit bisection(array,val)
    print( find_nearest1(array,val))
    %timeit find_nearest1(array,val)
    print( find_nearest2(array,val))
    %timeit find_nearest2(array,val)
    print( find_nearest3(array,val))
    %timeit find_nearest3(array,val)
    print( find_nearest4(array,val))
    %timeit find_nearest4(array,val)
    print( find_nearest5(array,val))
    %timeit find_nearest5(array,val)
    print( find_nearest6(array,val))
    %timeit find_nearest6(array,val)
    
    (50000, 50000)
    100000 loops, best of 3: 4.4 µs per loop
    50001
    1 loop, best of 3: 180 ms per loop
    50001
    1000 loops, best of 3: 267 µs per loop
    [50000]
    1000 loops, best of 3: 390 µs per loop
    50001
    1000 loops, best of 3: 259 µs per loop
    50001
    1000 loops, best of 3: 1.21 ms per loop
    [50000]
    1000 loops, best of 3: 746 µs per loop
    

    For a large array bisection gives 4us compared to next best 180us and longest 1.21ms (~100 - 1000 times faster). For smaller arrays it's ~2-100 times faster.

提交回复
热议问题