简体   繁体   中英

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. 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.

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.

So how do you define 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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