在Python中找到欧氏距离的最快方法

api*_*i55 3 python numpy scipy euclidean-distance

我有 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
Run Code Online (Sandbox Code Playgroud)

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

预先感谢您的回答

Fre*_*Foo 6

>>> 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]
Run Code Online (Sandbox Code Playgroud)

给你你想要的积分。