简体   繁体   English

如何使用Python查找图形中最接近的坐标

[英]How to find the nearest coordinate in graph using Python

I want to find the nearest graph points using its coordinate values(x,y). 我想使用其坐标值(x,y)查找最近的图形点。 My Dataset is like 我的数据集就像

-15907.500 -19367.500
-15912.500 -19362.500
-15907.500 -19362.500
-15917.500 -19357.500
-15912.500 -19357.500
-15917.500 -19352.500
-15912.500 -19352.500 
-16092.500 -19347.500 

.For example we can take any point as a reference and by this reference We have to find closest near and another little distance coordinate and like that. 例如,我们可以将任何点作为参考,并且通过该参考,我们必须找到最接近的点和另一个小距离的坐标,像这样。

data
      cols    cols1
0 -15907.5 -19367.5
1 -15912.5 -19362.5
2 -15907.5 -19362.5
3 -15917.5 -19357.5
4 -15912.5 -19357.5
5 -15917.5 -19352.5
6 -15912.5 -19352.5
7 -16092.5 -19347.5
from sklearn.metrics.pairwise import euclidean_distances
euclidean_distances(data)
array([[  0.        ,   7.07106781,   5.        ,  14.14213562,
         11.18033989,  18.02775638,  15.8113883 , 186.07794066],
       [  7.07106781,   0.        ,   5.        ,   7.07106781,
          5.        ,  11.18033989,  10.        , 180.62391868],
       [  5.        ,   5.        ,   0.        ,  11.18033989,
          7.07106781,  14.14213562,  11.18033989, 185.60711193],
       [ 14.14213562,   7.07106781,  11.18033989,   0.        ,
          5.        ,   5.        ,   7.07106781, 175.28548143],
       [ 11.18033989,   5.        ,   7.07106781,   5.        ,
          0.        ,   7.07106781,   5.        , 180.27756377],
       [ 18.02775638,  11.18033989,  14.14213562,   5.        ,
          7.07106781,   0.        ,   5.        , 175.071414  ],
       [ 15.8113883 ,  10.        ,  11.18033989,   7.07106781,
          5.        ,   5.        ,   0.        , 180.06943105],
       [186.07794066, 180.62391868, 185.60711193, 175.28548143,
        180.27756377, 175.071414  , 180.06943105,   0.        ]])

Reference:- 参考:-

  1. Eucledian_distance Eucledian_distance

To calculate the (Euclidean) distance between two points, you just subtract their coordinates, square, add, take the square root. 要计算两个点之间的(欧几里得)距离,只需减去它们的坐标,平方,加,取平方根即可。 In other words: 换一种说法:

def distance_between(x1, y1, x2, y2):
    return sqrt((x1-x2)**2 + (y1-y2)**2)

Conveniently, the math module provides the function hypot that does this for us (a bit more efficiently since it uses C). 方便地, math模块提供了功能hypot ,可以为我们完成此任务(由于使用C,因此效率更高)。

So now we can do this: 所以现在我们可以这样做:

from math import hypot

def closest_to(data, target):
    x, y = target # Break `target` into coordinates

    def distance(other_point): # How far is `other_point` from `target`?
        x2, y2 = other_point # Break `other_point` into coordinates
        return hypot(x-x2, y-y2) # Then calculate distance

    return min(data, key=distance) # Now run through `data` and return whichever one has the lowest `distance` result

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

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