为什么欧几里得距离在这里不起作用?

问题描述 投票:3回答:3

我有一组数据点(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)  

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

“在此处输入图像描述”

python distance
3个回答
2
投票

正如评论中已经指出的那样,困惑来自scale的巨大差异。尝试按各自的最大值对两个比例进行归一化,您会看到更清晰的图像:

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) 

enter image description here


0
投票

索引ind实际上是错误的,ind应该为59,但是它为53。这就是为什么它不起作用的原因。


0
投票

整个想法是通过单击鼠标以删除一些数据点来获得(x0,y0),但是按照之前所说的重新缩放,以从左到右的顺序删除这些点,而不是靠近鼠标位置的点。

© www.soinside.com 2019 - 2024. All rights reserved.