简体   繁体   English

在n元素对象上使用最近邻搜索

[英]Using Nearest Neighbor search on an n element object

I am looking for a way to use Nearest Neighbor search on an n element object in python. 我正在寻找一种在python中的n元素对象上使用近邻搜索的方法。 I want to have my parent object and then order the others based on nearest elements to it from nearest to farthest. 我想要我的父对象,然后根据从其最近到最远的元素对其他对象进行排序。 so can example would be: 这样的例子可以是:

Parents: 1, 1, 1, 1, 1

and the other objects say 而其他对象说

O1 = 1, 2, 2, 1, 2
O2 = 5, 5, 5, 5, 5
O3 = 3, 3, 3, 3, 3

so I would want it to return O1 as most relevant and O2 as least relevant. 所以我希望它返回O1最相关,O2最不相关。

So not sure what packages and algorithm to use. 所以不确定使用什么软件包和算法。

Here's a starting point: 这是一个起点:

def get_nearest(src, others):
    nearest = None
    nearest_dist = float("inf") # Some huge number that everything is less than
    for i in others:
        dist = metric(src, i) # But what is metric?
        if dist < nearest_dist:
            nearest = i
            nearest_dist = dist

    return nearest

This returns the closest match, according to some function metric that takes in two objects and returns some distance value. 根据接受两个对象并返回某个距离值的某些功能metric ,这将返回最接近的匹配项。

So how do you define metric ? 那么如何定义metric Well, that depends. 好吧,这取决于。 There's several ways of doing it, and you need to choose the one that most fits what data you're working with and what constitutes two objects being "close". 有多种实现方法,您需要选择最适合您正在使用的数据以及构成“关闭”两个对象的数据的一种方法。

For your list of numbers, you can try selecting the object with the least average difference between corresponding values: 对于数字列表,可以尝试选择对应值之间的平均差最小的对象:

def metric(a, b):
    s = 0
    for x, y in zip(a,b):
        s += abs(x-y)
    return s / len(a)

You can also do something more complex, like the root-mean-square average of the differences, or applying an exponential function to the differences so that outliers will stand out more. 您还可以执行更复杂的操作,例如差异的均方根平均值,或对差异应用指数函数,以使异常值更加突出。 It all depends on what you want to do with the data. 这完全取决于您要对数据进行的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM