[英]fastest way to find euclidean distance in python
我有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
我正在将python 2.7与numpy和scipy一起使用。 还有另一种方法可以使我的速度大大提高吗?
预先感谢您的回答
>>> 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]
给你你想要的点。
如果您有很多点,则可以计算加法和减法aplha的x和y边界,然后从位于该边界之外的特定考虑中消除b中的所有点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.