识别具有最小欧氏距离的点
我有一个 n 维点的集合,我想找到哪 2 个最接近。我能想到的最好的二维是:
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]
但这对于大型数组来说太慢了。我可以对其应用什么样的优化?
相关:
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 )
which gives
[8, 0, 1]
But this is too slow for large arrays. What kind of optimisation can I apply to it?
RELATED:
Euclidean distance between points in two different Numpy arrays, not within
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试 scipy.spatial.distance.pdist(myArr) 。这将为您提供一个压缩距离矩阵。您可以对其使用 argmin 并找到最小值的索引。这可以被转换成配对信息。
Try
scipy.spatial.distance.pdist(myArr)
. This will give you a condensed distance matrix. You can useargmin
on it and find the index of the smallest value. This can be converted into the pair information.关于这个问题有一个完整的维基百科页面,请参阅:http://en.wikipedia.org/wiki/Closest_pair_of_points
摘要:您可以使用递归分治算法实现 O(n log n)(在上面的 Wiki 页面上概述)。
There's a whole Wikipedia page on just this problem, see: http://en.wikipedia.org/wiki/Closest_pair_of_points
Executive summary: you can achieve O(n log n) with a recursive divide and conquer algorithm (outlined on the Wiki page, above).
您可以利用最新版本的 SciPy (v0.9) Delaunay 三角测量工具。您可以确定最近的两个点将是三角剖分中单纯形的边,这是比执行每个组合要小得多的对的子集。
这是代码(针对一般 ND 进行了更新):
看起来很接近 O(n):
You could take advantage of the latest version of SciPy's (v0.9) Delaunay triangulation tools. You can be sure that the closest two points will be an edge of a simplex in the triangulation, which is a much smaller subset of pairs than doing every combination.
Here's the code (updated for general N-D):
Seems closely O(n):
有一个 scipy 函数
pdist
可以以相当有效的方式获取数组中点之间的成对距离:http://docs.scipy.org/doc/scipy/reference/spatial.distance.html
输出 N*(N-1)/ 2 个唯一对(因为 r_ij == r_ji)。然后,您可以搜索最小值并避免代码中的整个循环混乱。
There is a scipy function
pdist
that will get you the pairwise distances between points in an array in a fairly efficient manner:http://docs.scipy.org/doc/scipy/reference/spatial.distance.html
that outputs the N*(N-1)/2 unique pairs (since r_ij == r_ji). You can then search on the minimum value and avoid the whole loop mess in your code.
也许您可以沿着这些思路继续进行:
有了更多的点,您需要能够以某种方式利用集群的层次结构。
Perhaps you could proceed along these lines:
With substantially more points you need to be able to somehow utilize the hierarchical structure of your clustering.
与仅执行嵌套循环并跟踪最短对相比,它有多快?我认为创建一个巨大的交叉数组可能会伤害你。如果你只处理二维点,即使 O(n^2) 仍然相当快。
How fast is it compared to just doing a nested loop and keeping track of the shortest pair? I think creating a huge cross array is what might be hurting you. Even O(n^2) is still pretty quick if you're only doing 2 dimensional points.
对于小型数据集,接受的答案是可以的,但其执行时间为
n**2
。然而,正如 @payne 所指出的,最佳解决方案可以实现 n*log(n) 计算时间缩放。这个最佳解决方案可以使用 sklearn.neighbors.BallTree如下。
对于非常大的
xy
值集,甚至对于大尺寸dim
(尽管示例说明了dim=2
的情况),此过程可以很好地扩展。结果输出如下所示使用 scipy.spatial.cKDTree,通过将
sklearn
导入替换为以下 Scipy 导入。但请注意,与BallTree
不同,cKDTree
不能很好地适应高维度The accepted answer is OK for small datasets, but its execution time scales as
n**2
. However, as pointed out by @payne, an optimal solution can achieven*log(n)
computation time scaling.This optial solution can be obtained using sklearn.neighbors.BallTree as follows.
This procedure scales well for very large sets of
xy
values and even for large dimensionsdim
(altough the example illustrates the casedim=2
). The resulting output looks like thisAn identical solution can be obtained using scipy.spatial.cKDTree, by replacing the
sklearn
import with the following Scipy one. Note however thatcKDTree
, unlikeBallTree
, does not scale well for high dimensions