【问题标题】:fastest way to find euclidean distance in python在python中找到欧几里得距离的最快方法
【发布时间】:2013-10-20 09:13:28
【问题描述】:

我有 2 组 2D 点(A 和 B),每组大约有 540 个点。我需要找到集合 B 中与 A 中所有点的定义距离 alpha 远的点。

我有一个解决方案,但不够快

# find the closest point of each of the new point to the target set
def find_closest_point( self, A, B):
    outliers = []
    for i in range(len(B)):
        # find all the euclidean distances
        temp = distance.cdist([B[i]],A)
        minimum = numpy.min(temp)
        # if point is too far away from the rest is consider outlier
        if minimum > self.alpha :
            outliers.append([i, B[i]])
        else:
            continue
    return outliers

我正在使用带有 numpy 和 scipy 的 python 2.7。是否有另一种方法可以使我的速度显着提高?

提前感谢您的回答

【问题讨论】:

标签: python numpy scipy euclidean-distance


【解决方案1】:
>>> from scipy.spatial.distance import cdist
>>> A = np.random.randn(540, 2)
>>> B = np.random.randn(540, 2)
>>> alpha = 1.
>>> ind = np.all(cdist(A, B) > alpha, axis=0)
>>> outliers = B[ind]

给你你想要的分数。

【讨论】:

  • 其实这给了我一组真假,虽然我认为我知道你想要做什么,实际上是一个巨大的速度提升。
  • 现在它工作正常,而且我的速度 aprox 有了很大的提高。整个算法 10 次尝试从 9.5 到 0.6,谢谢
【解决方案2】:

如果您有一组非常大的点,您可以计算 a 加减 aplha 的 x 和 y 边界,然后从特定考虑中消除位于该边界之外的 b 中的所有点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2013-03-02
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多