查找最接近数组2元素的数组1的元素 [英] Find elements of array one nearest to elements of array two

查看:90
本文介绍了查找最接近数组2元素的数组1的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案说明了如何找到距单点最近的(排序的)数组元素,以一种有效的方式处理大型数组(稍作修改):

This answer explains how to find the nearest (sorted) array element to a single point, in a manner efficient for large arrays (slightly modified):

def arg_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 idx-1
    else:
        return idx

如果相反,我们想查找最接近点的的数组元素(即第二个数组);除了使用for循环之外,是否有有效的方法(对于大型阵列来说,是速度)?

If, instead, we want to find the array elements nearest a set of points (i.e. a second array); are there efficient (by speed, for large arrays) ways of extending this besides using a for-loop?

一些测试用例:

>>> xx = [0.2, 0.8, 1.3, 1.5, 2.0, 3.1, 3.8, 3.9, 4.5, 5.1, 5.5]
>>> yy = [1, 2, 3, 4, 5]
>>> of_x_nearest_y(xx, yy)
[0.5, 2.0, 3.1, 3.9, 5.1]

>>> xx = [0.2, 0.8, 1.3, 1.5, 2.0, 3.1, 3.8, 3.9, 4.5, 5.1, 5.5]
>>> yy = [-2, -1, 4.6, 5.8]
>>> of_x_nearest_y(xx, yy)
[0.2, 0.2, 4.5, 5.5]


假设两个数组都已排序,则通过排除低于已匹配值(即 little 比 completely 天真for循环更好>


assuming both arrays are sorted, you can do a little better than a completely naive for-loop by excluding values below those already matched, i.e.

def args_nearest(options, targets):
    locs = np.zeros(targets.size, dtype=int)
    prev = 0
    for ii, tt in enumerate(targets):
        locs[ii] = prev + arg_nearest(options[prev:], tt)
        prev = locs[ii]
    return locs

推荐答案

您可以进行一些更改以将其扩展为value中的元素数组,就像这样-

You can make few changes to extend it for an array of elements in value, like so -

idx = np.searchsorted(xx, yy, side="left").clip(max=xx.size-1)
mask = (idx > 0) &  \
       ( (idx == len(xx)) | (np.fabs(yy - xx[idx-1]) < np.fabs(yy - xx[idx])) )
out = xx[idx-mask]

说明

命名法:array是我们要在其中放置value中的元素以保持array排序性质的数组.

Nomenclature : array is the array in which we are looking to place elements from value to maintain the sorted nature of array.

需要进行更改,以将单个元素的解决方案扩展到许多要搜索的元素:

Changes needed to extend the solution for a single element to many elements for searching :

1]最多裁剪从np.searchsorted获得的索引数组idx. array.size-1的元素,因为对于value中大于array最大值的元素,我们需要使idx可被array索引.

1] Clip the indices array idx obtained from np.searchsorted at a max. of array.size-1, because for elements in value that are larger than the maximum of array, we need to make idx indexable by array.

2]引入numpy代替math以矢量化的方式进行这些操作.

2] Introduce numpy to replace math to do those operations in a vectorized manner.

3]用idx - mask的技巧替换条件语句.在这种情况下,Python内部会将mask上转换为int数组,以与idx的数据类型匹配.因此,所有True元素都变为1,因此对于True元素,我们将有效地具有idx-1,这是原始代码中IF条件语句的True情况.

3] Replace the conditional statement by the trick of idx - mask. In this case, internally Python would up-convert mask to an int array to match up with the datatype of idx. Thus, all the True elements become 1 and thus for True elements we would effectively have idx-1, which is the True case of the IF conditional statement in the original code.

这篇关于查找最接近数组2元素的数组1的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆