Frage

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

War es hilfreich?

Lösung

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

Andere Tipps

If you have a very large set of points you could calculate x & y bounds of a add & subtract aplha then eliminate all the points in b from specific consideration that lay outside of that boundary.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top