欧几里德计算中数组元素的求取

2024-10-20 03:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和numpy还不熟悉。我得计算两点之间的欧几里德距离。我的第一点是随机生成的

x1[35,60]

x2= [[35 60]][[26 84][69 67][80 63][11 96][44 22][17 76][57 95][44 81][10 56][29 33]]

我已经成功地计算了最小欧氏距离=22.847,并且发现最小欧氏距离在x1[35,60]和x2[44,81]两点之间。但我通过打印值并手动查看发现了这一点。我想把x2(它给出了最小欧氏距离)存储在某个变量中,以便以后使用。我如何做到这一点

这是我的密码

从随机导入randint as r 导入numpy 从数学导入

x = r(1, 100)
y = r(1, 100)
isFailedTest = (5<=x<=15) and (10<=y<=11)
selected_test = []
selected_test.append ((x,y))
candidate = [ (r(1,100), r(1,100)) for i in xrange(10)]    

x1= numpy.array(selected_test)
x2= numpy.array(candidate)

dists = numpy.hypot(x1[:, 0, numpy.newaxis]-x2[:, 0], x1[:, 1, numpy.newaxis]-x2[:, 1]) 
mindist = numpy.min(dists, axis=1)
minid   = numpy.argmin(dists, axis=1)

print mindist
print candidate
print x1
print x2
print minid

我想我就快到了,因为我可以看到打印输出的最小距离。但我不能通过编程的方式访问给出最小值的点。如有任何帮助建议,我们将不胜感激。谢谢你


Tags: testnumpy距离手动arraycandidateprintx1