在python中找到两个列表之间的最低最近邻居的索引 [英] Find the indices of the lowest closest neighbors between two lists in python

查看:305
本文介绍了在python中找到两个列表之间的最低最近邻居的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出2个不等大小的numpy数组:A(预排序的数据集)和B(查询值列表).我想在数组A中找到最接近数组B的每个较低"的邻居.下面的示例代码:

Given 2 numpy arrays of unequal size: A (a presorted dataset) and B (a list of query values). I want to find the closest "lower" neighbor in array A to each element of array B. Example code below:

import numpy as np

A = np.array([0.456, 2.0, 2.948, 3.0, 7.0, 12.132]) #pre-sorted dataset
B = np.array([1.1, 1.9, 2.1, 5.0, 7.0]) #query values, not necessarily sorted
print A.searchsorted(B)
# RESULT:  [1 1 2 4 4]
# DESIRED: [0 0 1 3 4]

在此示例中,B [0]的最近邻居是A [0]和A [1].它最接近A [1],这就是为什么searchsorted返回索引1作为匹配项,但是我想要的是索引0的下一个邻居.对于B [1:4]相同,并且B [4]应该与A [4],因为两个值都相同.

In this example, B[0]'s closest neighbors are A[0] and A[1]. It is closest to A[1], which is why searchsorted returns index 1 as a match, but what i want is the lower neighbor at index 0. Same for B[1:4], and B[4] should be matched with A[4] because both values are identical.

我可以做一些笨拙的事情:

I could do something clunky like this:

desired = []
for b in B:
    id = -1
    for a in A:
        if a > b:
            if id == -1:
                desired.append(0)
            else:
                desired.append(id)
            break

        id+=1

print desired
# RESULT: [0, 0, 1, 3, 4]

但是有一种更简洁的方法可以用numpy编写.我想将解决方案保留在numpy中,因为我正在处理大型数据集,但是我愿意接受其他选择.

But there's gotta be a prettier more concise way to write this with numpy. I'd like to keep my solution in numpy because i'm dealing with large data sets, but i'm open to other options.

推荐答案

您可以引入可选参数side并将其设置为'right',如

You can introduce the optional argument side and set it to 'right' as mentioned in the docs. Then, subtract the final indices result by 1 for the desired output, like so -

A.searchsorted(B,side='right')-1

样品运行-

In [63]: A
Out[63]: array([  0.456,   2.   ,   2.948,   3.   ,   7.   ,  12.132])

In [64]: B
Out[64]: array([ 1.1,  1.9,  2.1,  5. ,  7. ])

In [65]: A.searchsorted(B,side='right')-1
Out[65]: array([0, 0, 1, 3, 4])

In [66]: A.searchsorted(A,side='right')-1 # With itself
Out[66]: array([0, 1, 2, 3, 4, 5])

这篇关于在python中找到两个列表之间的最低最近邻居的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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