识别点具有最小欧几里得距离 [英] Identifying points with the smallest Euclidean distance
本文介绍了识别点具有最小欧几里得距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有n个三维点的集合,我想要找到其中2个是最接近。我可以拿出2的尺寸最好是:
I have a collection of n dimensional points and I want to find which 2 are the closest. The best I could come up for 2 dimensions is:
from numpy import *
myArr = array( [[1, 2],
[3, 4],
[5, 6],
[7, 8]] )
n = myArr.shape[0]
cross = [[sum( ( myArr[i] - myArr[j] ) ** 2 ), i, j]
for i in xrange( n )
for j in xrange( n )
if i != j
]
print min( cross )
这使得
[8, 0, 1]
但是,这是对于大型阵列太慢。我能将它什么样的优化?
But this is too slow for large arrays. What kind of optimisation can I apply to it?
相关报道:
推荐答案
尝试 scipy.spatial.distance.pdist(myArr,该)
。这会给你一个浓缩的距离矩阵。您可以使用 argmin
就可以了,找到最小的值的索引。这可以转换成一对信息。
Try scipy.spatial.distance.pdist(myArr)
. This will give you a condensed distance matrix. You can use argmin
on it and find the index of the smallest value. This can be converted into the pair information.
这篇关于识别点具有最小欧几里得距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文