Python 在numpy数组中查找最近的值
是否有一种简单的方法(例如函数)来查找数组中最近的值 例如:Python 在numpy数组中查找最近的值,python,search,numpy,Python,Search,Numpy,是否有一种简单的方法(例如函数)来查找数组中最近的值 例如: np.find_nearest( array, value ) 稍加修改,上述答案适用于任意尺寸的阵列(1d、2d、3d等): 或者,写为一行: a.flat[np.abs(a - a0).argmin()] 下面是一个处理非标量“值”数组的版本: 或者如果输入为标量,则返回数值类型(例如int、float)的版本: def find_nearest(array, values): values = np.atleast_
np.find_nearest( array, value )
稍加修改,上述答案适用于任意尺寸的阵列(1d、2d、3d等): 或者,写为一行:
a.flat[np.abs(a - a0).argmin()]
下面是一个处理非标量“值”数组的版本: 或者如果输入为标量,则返回数值类型(例如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]
这是一个扩展,用于在向量数组中查找最近的向量
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])
如果您不想使用numpy,可以这样做:
def find_nearest(array, value):
n = [abs(i-value) for i in array]
idx = n.index(min(n))
return array[idx]
如果阵列已排序且非常大,则这是一个更快的解决方案:
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]
def find_最近(数组,值):
idx=np.searchsorted(数组,值,side=“left”)
如果idx>0且(idx==len(array)或math.fabs(value-array[idx-1])
这可以扩展到非常大的阵列。如果不能假定数组已经排序,则可以轻松修改上述内容以在方法中排序。对于小型阵列来说,这是一种过分的杀伤力,但一旦它们变大,这会快得多。对于大型阵列,@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
def find_idx_nearest_val(数组,值):
idx_sorted=np.argsort(数组)
排序数组=np.array(数组[idx\u排序])
idx=np.searchsorted(排序的数组,值,side=“left”)
如果idx>=len(数组):
最近的idx_=已排序的idx_[len(数组)-1]
elif idx==0:
最近的idx_=已排序的idx_[0]
其他:
如果abs(值排序数组[idx-1])
这是一个使用scipy的版本,用于@Ari Onasafari,回答“要在向量数组中找到最近的向量”
[1]中的:来自scipy导入空间
在[2]中:将numpy作为np导入
[3]中:A=np.random.random((10,2))*100
在[4]中:A
出[4]:
数组([[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]])
在[5]中:pt=[6,30]#答案摘要:如果一个人有一个排序的数组
,那么对分代码(如下所示)执行得最快~大型阵列快100-1000倍,小型阵列快2-100倍。它也不需要numpy。
如果你有一个未排序的<代码>数组< /代码>,那么如果<代码>数组是大的,你应该首先考虑使用O(n log n)排序,然后二等分,如果<代码>数组是小的,那么方法2似乎是最快的。
首先,您应该澄清最近值的含义。通常需要横坐标中的间隔,例如数组=[0,0.7,2.1],值=1.95,答案是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
对于大型阵列,二等分提供4us,次优180 us,最长1.21ms(快约100-1000倍)。对于较小的阵列,其速度约为2-100倍。我认为最适合的方式是:
num = 65 # Input number
array = n.random.random((10))*100 # Given array
nearest_idx = n.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)
这是基本代码。如果需要,可以将其用作函数。如果要搜索多个值(值
可以是多维数组),则这里有@Dimitri解决方案的快速矢量化版本:
所有的答案都有助于收集信息以编写高效的代码。然而,我已经编写了一个小型Python脚本,以针对各种情况进行优化。如果对提供的数组进行排序,这将是最好的情况。如果搜索指定值的最近点的索引,则对分
模块的时间效率最高。当一次搜索索引对应于一个数组时,numpy searchsorted
效率最高
import numpy as np
import bisect
xarr = np.random.rand(int(1e7))
srt_ind = xarr.argsort()
xar = xarr.copy()[srt_ind]
xlist = xar.tolist()
bisect.bisect_left(xlist, 0.3)
在[63]中:%时间对分。左对分(xlist,0.3)
CPU时间:用户0纳秒,系统0纳秒,总计0纳秒
壁时间:22.2µs
np.searchsorted(xar, 0.3, side="left")
在[64]:%time np.searchsorted(xar,0.3,side=“left”)
CPU时间:用户0纳秒,系统0纳秒,总计0纳秒
壁时间:98.9µs
randpts = np.random.rand(1000)
np.searchsorted(xar, randpts, side="left")
%时间np.searchsorted(xar,randpts,side=“left”)
CPU时间:用户4毫秒,系统:0纳秒,总计:4毫秒
壁时间:1.2毫秒
如果我们遵循乘法规则,则numpy应花费约100 ms,这意味着更快约83倍。这是以下的矢量化版本:
import numpy as np
def find_nearest(array, value):
array = np.array(array)
z=np.abs(array-value)
y= np.where(z == z.min())
m=np.array(y)
x=m[0,0]
y=m[1,0]
near_value=array[x,y]
return near_value
array =np.array([[60,200,30],[3,30,50],[20,1,-50],[20,-500,11]])
print(array)
value = 0
print(find_nearest(array, value))
可能有助于ndarrays
:
def find_nearest(X, value):
return X[np.unravel_index(np.argmin(np.abs(X - value)), X.shape)]
对于二维阵列,要确定最近元素的i,j位置:
import numpy as np
def find_nearest(a, a0):
idx = (np.abs(a - a0)).argmin()
w = a.shape[1]
i = idx // w
j = idx - i * w
return a[i,j], i, j
这是一个适用于二维阵列的版本,如果用户有scipy函数,则使用它;如果用户没有,则使用更简单的距离计算
默认情况下,输出是最接近您输入的值的索引,但您可以使用output
关键字将其更改为'index'
,'value'
或'both'中的一个<
#`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]
>>> %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
import numpy as np
import bisect
xarr = np.random.rand(int(1e7))
srt_ind = xarr.argsort()
xar = xarr.copy()[srt_ind]
xlist = xar.tolist()
bisect.bisect_left(xlist, 0.3)
np.searchsorted(xar, 0.3, side="left")
randpts = np.random.rand(1000)
np.searchsorted(xar, randpts, side="left")
import numpy as np
def find_nearest(array, value):
array = np.array(array)
z=np.abs(array-value)
y= np.where(z == z.min())
m=np.array(y)
x=m[0,0]
y=m[1,0]
near_value=array[x,y]
return near_value
array =np.array([[60,200,30],[3,30,50],[20,1,-50],[20,-500,11]])
print(array)
value = 0
print(find_nearest(array, value))
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)
def find_nearest(X, value):
return X[np.unravel_index(np.argmin(np.abs(X - value)), X.shape)]
import numpy as np
def find_nearest(a, a0):
idx = (np.abs(a - a0)).argmin()
w = a.shape[1]
i = idx // w
j = idx - i * w
return a[i,j], i, j
def find_nearest_2d(array, value, kind='cdist', output='index'):
# 'array' must be a 2D array
# 'value' must be a 1D array with 2 elements
# 'kind' defines what method to use to calculate the distances. Can choose one
# of 'cdist' (default) or 'euclidean'. Choose 'euclidean' for very large
# arrays. Otherwise, cdist is much faster.
# 'output' defines what the output should be. Can be 'index' (default) to return
# the index of the array that is closest to the value, 'value' to return the
# value that is closest, or 'both' to return index,value
import numpy as np
if kind == 'cdist':
try: from scipy.spatial.distance import cdist
except ImportError:
print("Warning (find_nearest_2d): Could not import cdist. Reverting to simpler distance calculation")
kind = 'euclidean'
index = np.where(array == value)[0] # Make sure the value isn't in the array
if index.size == 0:
if kind == 'cdist': index = np.argmin(cdist([value],array)[0])
elif kind == 'euclidean': index = np.argmin(np.sum((np.array(array)-np.array(value))**2.,axis=1))
else: raise ValueError("Keyword 'kind' must be one of 'cdist' or 'euclidean'")
if output == 'index': return index
elif output == 'value': return array[index]
elif output == 'both': return index,array[index]
else: raise ValueError("Keyword 'output' must be one of 'index', 'value', or 'both'")
import numpy as np
def find_nearest(array, value, k):
array = np.asarray(array)
idx = np.argsort(abs(array - value))[:k]
return array[idx]