在numpy数组中查找最接近的值

Foo*_*chu 302 python search numpy

是否有一种numpy-thonic方式,例如函数,来查找数组中最接近的值

例:

np.find_nearest( array, value )
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 470

import numpy as np
def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return array[idx]

array = np.random.random(10)
print(array)
# [ 0.21069679  0.61290182  0.63425412  0.84635244  0.91599191  0.00213826
#   0.17104965  0.56874386  0.57319379  0.28719469]

value = 0.5

print(find_nearest(array, value))
# 0.568743859261
Run Code Online (Sandbox Code Playgroud)

  • @EOL:`return np.abs(array-value).min()`给出了错误的答案.这给出了绝对值距离的min,并且我们需要以某种方式返回实际的数组值.我们可以添加"值"并接近,但是绝对值会让事情变得棘手...... (50认同)
  • 看起来很疯狂没有一个numpy内置这样做. (22认同)
  • @~unutbu你是对的,我的坏.我想不出比你的解决方案更好的东西! (9认同)
  • 大胖警告:如果您的数据包含 np.nan,这些点将始终显示为最接近的。 (8认同)
  • @johanvdw 哇,这几乎应该算是一个错误。要修复此问题,请将“np.argmin()”替换为“np.nanargmin()”,它就可以工作。 (6认同)
  • @jsmedmar二分法(见下面的答案)是O(log(n)). (3认同)
  • `FutureWarning:不建议使用'argmin'。请改用“ idxmin”。argmin的行为将得到纠正,以在将来返回位置最小值。使用'series.values.argmin'立即获取最小值的位置。`对于上面的解决方案,使用`idxmin`而不是`argmin`对我有效。(v3.6.4) (3认同)
  • 有人可以解释这个算法的时间复杂度是多少? (2认同)
  • @jsmedmar`O(n)`除非做了一些奇怪的事情:`n`操作应用向量化减法,`n`操作找到最小的,直到'n`操作来获得基于索引的元素.`O(3n)= O(n)` (2认同)
  • @jorijnsmit:为了使代码与 Pandas 系列兼容,我添加了 `array = np.asarray(array)`。这会将系列转换为 NumPy 数组。然后调用 NumPy 的 [`argmin`](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.argmin.html) 将按预期工作。我不能将它更改为 `idxmin`,因为如果给 `find_nearest` 传递一个 NumPy 数组,那会破坏代码。 (2认同)

Dem*_*tri 75

如果您的数组已排序且非常大,这是一个更快的解决方案:

def find_nearest(array,value):
    idx = np.searchsorted(array, value, side="left")
    if idx > 0 and (idx == len(array) or math.fabs(value - array[idx-1]) < math.fabs(value - array[idx])):
        return array[idx-1]
    else:
        return array[idx]
Run Code Online (Sandbox Code Playgroud)

这可以扩展到非常大的数组.如果您不能假设数组已经排序,则可以轻松修改上述内容以在方法中进行排序.这对于小型阵列来说太过分了,但是一旦它们变大,这就会快得多.

  • @Michael对于单个值,Numpy数学例程将比`math`例程慢,请参阅[this answer](http://stackoverflow.com/questions/3650194/are-numpys-math-functions-faster-than- python)。 (2认同)
  • 如果您想要一次查找多个值(通过一些调整),这是最佳解决方案.整个`if/else`需要替换为`idx = idx - (np.abs(value - array [idx-1])<np.abs(value - array [idx])); return array [idx]` (2认同)
  • 这很好,但如果`value`大于`array`的最大元素,则不起作用.我将`if`语句更改为`if idx == len(array)或math.fabs(value - array [idx - 1])<math.fabs(value - array [idx])`以使其对我有用! (2认同)
  • 当idx为0时,这不起作用.if应该是:`if idx> 0和(idx == len(array)或math.fabs(value - array [idx-1])<math.fabs(value - 阵列[IDX)):` (2认同)

kwg*_*man 48

稍作修改,上面的答案适用于任意维度的数组(1d,2d,3d,...):

def find_nearest(a, a0):
    "Element in nd array `a` closest to the scalar value `a0`"
    idx = np.abs(a - a0).argmin()
    return a.flat[idx]
Run Code Online (Sandbox Code Playgroud)

或者,写成一行:

a.flat[np.abs(a - a0).argmin()]
Run Code Online (Sandbox Code Playgroud)

  • 请提供一个示例,其中建议的答案不起作用.如果你找到一个,我会修改我的答案.如果你找不到,那么你可以删除你的评论吗? (9认同)
  • "扁平"位不是必需的.`a [np.abs(a-a0).argmin)]`工作正常. (5认同)
  • 因此,它不适用于更高的维度,应该删除答案(或修改以反映这一点) (3认同)
  • 实际上,这仍然只适用于一个维度,因为argmin()为每个列/维度提供多个结果.我也有一个错字.这适用于至少2维:`a [np.sum(np.square(np.abs(a-a0)),1).argmin()]`. (2认同)

Jos*_*ert 17

答案摘要:如果有一个已排序,array则二分码(下面给出)执行速度最快.大型阵列的速度提高约100-1000倍,小型阵列的速度提高约2-100倍.它也不需要numpy.如果你有一个未排序的array那么if array是大的,应该考虑首先使用O(n logn)排序然后二分,如果array小,那么方法2似乎是最快的.

首先,你应该用最接近的值来澄清你的意思.通常人们想要横坐标中的区间,例如array = [0,0.7,2.1],value = 1.95,answer将是idx = 1.这是我怀疑你需要的情况(否则,一旦找到间隔,可以使用后续条件语句很容易地修改以下内容).我会注意到执行此操作的最佳方式是二分(我将首先提供 - 注意它根本不需要numpy并且比使用numpy函数更快,因为它们执行冗余操作).然后我将提供与其他用户在此处呈现的其他人的时序比较.

二分法:

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
Run Code Online (Sandbox Code Playgroud)

现在我将从其他答案中定义代码,它们每个都返回一个索引:

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
Run Code Online (Sandbox Code Playgroud)

现在我将对代码进行计时: 注意方法1,2,4,5没有正确给出间隔.方法1,2,4舍入到阵列中的最近点(例如> = 1.5 - > 2),方法5总是向上舍入(例如1.45 - > 2).只有方法3和6,当然还有二分法才能正确地给出间隔.

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
Run Code Online (Sandbox Code Playgroud)

对于大阵列,二等分为4us,而下一个最佳为180us,最长为1.21ms(快~100-1000倍).对于较小的阵列,它的速度要快〜2-100倍.

  • python标准库已经包含在二分算法的实现中:https://docs.python.org/3.6/library/bisect.html (6认同)
  • 您假设数组已排序.有人不想对数组进行排序的原因有很多:例如,如果数组表示线图上的数据点. (2认同)
  • 这没有找到*nearest*值,它找到下一个最低值. (2认同)

小智 16

这是一个扩展,用于在向量数组中找到最近的向量.

import numpy as np

def find_nearest_vector(array, value):
  idx = np.array([np.linalg.norm(x+y) for (x,y) in array-value]).argmin()
  return array[idx]

A = np.random.random((10,2))*100
""" A = array([[ 34.19762933,  43.14534123],
   [ 48.79558706,  47.79243283],
   [ 38.42774411,  84.87155478],
   [ 63.64371943,  50.7722317 ],
   [ 73.56362857,  27.87895698],
   [ 96.67790593,  77.76150486],
   [ 68.86202147,  21.38735169],
   [  5.21796467,  59.17051276],
   [ 82.92389467,  99.90387851],
   [  6.76626539,  30.50661753]])"""
pt = [6, 30]  
print find_nearest_vector(A,pt)
# array([  6.76626539,  30.50661753])
Run Code Online (Sandbox Code Playgroud)


ryg*_*gyr 9

这是一个处理非标量"值"数组的版本:

import numpy as np

def find_nearest(array, values):
    indices = np.abs(np.subtract.outer(array, values)).argmin(0)
    return array[indices]
Run Code Online (Sandbox Code Playgroud)

或者,如果输入是标量,则返回数值类型的版本(例如int,float):

def find_nearest(array, values):
    values = np.atleast_1d(values)
    indices = np.abs(np.subtract.outer(array, values)).argmin(0)
    out = array[indices]
    return out if len(out) > 1 else out[0]
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案无法扩展。`np.subtract.outer` 将生成整个外积矩阵,如果 `array` 和/或 `values` 非常大,这真的很慢并且占用大量内存。 (2认同)

Nic*_*ord 9

如果您不想使用numpy,这将执行此操作:

def find_nearest(array, value):
    n = [abs(i-value) for i in array]
    idx = n.index(min(n))
    return array[idx]
Run Code Online (Sandbox Code Playgroud)


efi*_*ida 8

这是@Ari Onasafari的scipy版本,回答" 找到向量数组中最近的向量 "

In [1]: from scipy import spatial

In [2]: import numpy as np

In [3]: A = np.random.random((10,2))*100

In [4]: A
Out[4]:
array([[ 68.83402637,  38.07632221],
       [ 76.84704074,  24.9395109 ],
       [ 16.26715795,  98.52763827],
       [ 70.99411985,  67.31740151],
       [ 71.72452181,  24.13516764],
       [ 17.22707611,  20.65425362],
       [ 43.85122458,  21.50624882],
       [ 76.71987125,  44.95031274],
       [ 63.77341073,  78.87417774],
       [  8.45828909,  30.18426696]])

In [5]: pt = [6, 30]  # <-- the point to find

In [6]: A[spatial.KDTree(A).query(pt)[1]] # <-- the nearest point 
Out[6]: array([  8.45828909,  30.18426696])

#how it works!
In [7]: distance,index = spatial.KDTree(A).query(pt)

In [8]: distance # <-- The distances to the nearest neighbors
Out[8]: 2.4651855048258393

In [9]: index # <-- The locations of the neighbors
Out[9]: 9

#then 
In [10]: A[index]
Out[10]: array([  8.45828909,  30.18426696])
Run Code Online (Sandbox Code Playgroud)


aph*_*aph 7

对于大型阵列,@ Demitri给出的(优秀)答案远远快于目前标记为最佳的答案.我已经通过以下两种方式调整了他的确切算法:

  1. 无论输入数组是否已排序,下面的函数都有效.

  2. 下面的函数返回与最接近的值对应的输入数组的索引,这稍微更一般.

请注意,下面的函数还处理特定的边缘情况,这会导致@Demitri编写的原始函数中的错误.否则,我的算法与他的算法相同.

def find_idx_nearest_val(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
Run Code Online (Sandbox Code Playgroud)


Ish*_*mar 7

我认为最Pythonic的方式是:

 num = 65 # Input number
 array = np.random.random((10))*100 # Given array 
 nearest_idx = np.where(abs(array-num)==abs(array-num).min())[0] # If you want the index of the element of array (array) nearest to the the given number (num)
 nearest_val = array[abs(array-num)==abs(array-num).min()] # If you directly want the element of array (array) nearest to the given number (num)
Run Code Online (Sandbox Code Playgroud)

这是基本代码。如果需要,您可以将其用作函数


Sou*_*men 6

所有的答案都有利于收集信息以编写高效的代码。不过,我编写了一个小型 Python 脚本来针对各种情况进行优化。如果提供的数组已排序,这将是最好的情况。如果搜索指定值的最近点的索引,则bisect模块是最省时的。当搜索对应于数组的索引时,numpy searchsorted效率最高。

\n\n
import numpy as np\nimport bisect\nxarr = np.random.rand(int(1e7))\n\nsrt_ind = xarr.argsort()\nxar = xarr.copy()[srt_ind]\nxlist = xar.tolist()\nbisect.bisect_left(xlist, 0.3)\n
Run Code Online (Sandbox Code Playgroud)\n\n

在[63]中: %time bisect.bisect_left(xlist, 0.3)\n CPU 时间:用户 0 ns,系统:0 ns,总计:0 ns\n 运行时间:22.2 \xc2\xb5s

\n\n
np.searchsorted(xar, 0.3, side="left")\n
Run Code Online (Sandbox Code Playgroud)\n\n

在[64]中:%time np.searchsorted(xar, 0.3, side="left")\n CPU时间:用户0 ns,系统:0 ns,总计:0 ns\n Wall时间:98.9 \xc2\xb5s

\n\n
randpts = np.random.rand(1000)\nnp.searchsorted(xar, randpts, side="left")\n
Run Code Online (Sandbox Code Playgroud)\n\n

%time np.searchsorted(xar, randpts, side="left")\nCPU 时间:用户 4 毫秒,系统:0 纳秒,总计:4 毫秒\nWall 时间:1.2 毫秒

\n\n

如果我们遵循乘法规则,那么 numpy 应该需要约 100 毫秒,这意味着速度要快约 83 倍。

\n


ant*_*ell 5

如果您有很多values要搜索的东西,这是@Dimitri解决方案的快速向量化版本(values可以是多维数组):

#`values` should be sorted
def get_closest(array, values):
    #make sure array is a numpy array
    array = np.array(array)

    # get insert positions
    idxs = np.searchsorted(array, values, side="left")

    # find indexes where previous index is closer
    prev_idx_is_less = ((idxs == len(array))|(np.fabs(values - array[np.maximum(idxs-1, 0)]) < np.fabs(values - array[np.minimum(idxs, len(array)-1)])))
    idxs[prev_idx_is_less] -= 1

    return array[idxs]
Run Code Online (Sandbox Code Playgroud)

基准测试

比使用for@Demitri解决方案的循环快100倍以上

>>> %timeit ar=get_closest(np.linspace(1, 1000, 100), np.random.randint(0, 1050, (1000, 1000)))
139 ms ± 4.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

>>> %timeit ar=[find_nearest(np.linspace(1, 1000, 100), value) for value in np.random.randint(0, 1050, 1000*1000)]
took 21.4 seconds
Run Code Online (Sandbox Code Playgroud)

  • 第一个答案“正常”: `get_closest([1,5,10,20], [1,4,16]) -&gt; [1, 5, 20]`,这个应该有更多的赞成票。 (4认同)

Zha*_*hen 5

这是unutbu 答案的矢量化版本:

def find_nearest(array, values):
    array = np.asarray(array)

    # the last dim must be 1 to broadcast in (array - values) below.
    values = np.expand_dims(values, axis=-1) 

    indices = np.abs(array - values).argmin(axis=-1)

    return array[indices]


image = plt.imread('example_3_band_image.jpg')

print(image.shape) # should be (nrows, ncols, 3)

quantiles = np.linspace(0, 255, num=2 ** 2, dtype=np.uint8)

quantiled_image = find_nearest(quantiles, image)

print(quantiled_image.shape) # should be (nrows, ncols, 3)
Run Code Online (Sandbox Code Playgroud)


Gus*_*ava 5

也许有帮助ndarrays

def find_nearest(X, value):
    return X[np.unravel_index(np.argmin(np.abs(X - value)), X.shape)]
Run Code Online (Sandbox Code Playgroud)