简体   繁体   中英

fastest way to find euclidean distance in python

I have 2 sets of 2D points (A and B), each set have about 540 points. I need to find the points in set B that are farther than a defined distance alpha from all the points in A.

I have a solution, but is not fast enough

# 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

I am using python 2.7 with numpy and scipy. Is there another way to do this that I may gain a considerable increase in speed?

Thanks in advance for the answers

>>> 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]

gives you the points you want.

如果您有很多点,则可以计算加法和减法aplha的x和y边界,然后从位于该边界之外的特定考虑中消除b中的所有点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM