首页 > 解决方案 > 为什么欧几里得距离在这里不起作用?

问题描述

我有一组数据点(x,y),我想检索相对于一个给定坐标(x0,y0)的最近点。所以我估计所有点之间的距离并取较小的那个,但由于某种原因,它没有给出这个特定示例的最近点。

x = np.array([64.2242304 , 61.2518646 , 58.47390016, 64.20955699, 61.23787029,
   58.46054054, 64.19456598, 61.22357308, 58.44689175, 64.17925719,
   61.20897279, 58.43295363, 64.16385925, 61.19428749, 58.41893435,
   63.87231513, 60.91623635, 58.1534937 , 63.85128529, 60.89617978,
   58.13434676, 63.82993374, 60.87581641, 58.11490693, 63.80826025,
   60.85514599, 58.09517398, 63.78658575, 60.83447461, 58.07544011,
   62.63916359, 59.74015637, 57.03075264, 62.60371968, 59.70635284,
   56.99848221, 62.56794127, 59.6722303 , 56.96590723, 62.53182804,
   59.63778843, 56.93302741, 62.49537965, 59.6030269 , 56.89984243,
   61.81677277, 58.95582667, 56.28199476, 61.77417322, 58.91519867,
   56.24320937, 61.73123185, 58.87424468, 56.20411277, 61.68857821,
   58.83356509, 56.16527813, 61.6449571 , 58.79196282, 56.12556265])

y = np.array([0.873742  , 0.89044404, 0.8905113 , 0.84973851, 0.87760778,
   0.90631466, 0.87658266, 0.87059042, 0.87698866, 0.84016969,
   0.88174558, 0.88865462, 0.85846238, 0.90678905, 0.91368672,
   0.80547866, 0.85115265, 0.85444583, 0.86176715, 0.86161907,
   0.8497139 , 0.83833086, 0.8329039 , 0.8346713 , 0.81458452,
   0.85984806, 0.88086607, 0.85484444, 0.8459257 , 0.85321279,
   0.89137664, 0.95721649, 0.99768893, 0.95881713, 0.92558392,
   0.95388805, 0.92377277, 0.94814089, 0.99000415, 0.94148869,
   0.95076589, 0.97205063, 0.93682003, 0.94698105, 0.98542763,
   0.88543196, 0.88457109, 0.88873105, 0.9231422 , 0.92223527,
   0.90003003, 0.9508397 , 0.97875354, 0.95647845, 0.91495685,
   0.92922366, 0.93115852, 0.99404974, 0.99146021, 1.00299637])
x0, y0 = 56.25631332055924, 1.017018635355944 ### the closest point to this should be the last coordinate in (x,y)
ind = np.argmin(np.sqrt((x-x0)**2 + (y-y0)**2))
plt.plot(x,y,'o')  
plt.plot(x0,y0,'+') 
plt.plot(x[ind],y[ind],'+', ms=10)  

结果如下图。我想要离橙色十字最近的点,但我得到了绿色十字所在的点!知道为什么吗?

在此处输入图像描述

标签: pythondistance

解决方案


正如评论中已经指出的那样,混乱来自规模的巨大差异。尝试通过各自的最大值对两个比例进行归一化,您会发现错误的最近点是您认为的那个:

x_max = max(x)
y_max = max(y)
x /= x_max
x0 /= x_max
y /= y_max
y0 /= y_max

ind = np.argmin(np.sqrt((x-x0)**2 + (y-y0)**2))
plt.plot(x,y,'o')  
plt.plot(x0,y0,'+', ms=30) 
plt.plot(x[ind],y[ind],'+', ms=30) 

在此处输入图像描述

但这只是由于规模的差异,如前所述。具有最小欧几里德距离的点是图中突出显示的点。


推荐阅读