2013-10-20 25 views
2

我有2組2D點(A和B),每組有大約540點。我需要找到集合B是比定義的距離阿爾法遠離所有的點A.尋找蟒蛇中歐式距離的最快方法

我有一個解決方案的要點,但速度不夠快

# 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。有沒有另外一種方法可以使我的速度大大提高?

在此先感謝您的答案

+0

[兩個不同的Numpy數組中的點之間的歐氏距離的可能的重複,而不是](http://stackoverflow.com/questions/1871536/euclidean-distance-between-points-in-two-different-numpy-arrays -not-within)或[用numpy計算歐幾里德距離](http://stackoverflow.com/questions/1401712/calculate-euclidean-distance-with-numpy) –

+1

由於你似乎只尋找離羣值,這是最近的鄰居搜索。我認爲你可以(也應該)使用'scipy.spatial.KDTree'(舊的cKDTree可能不支持這一點,所以在cKDTree中使用一個更新的scipy)。 – seberg

+0

另外:http://stackoverflow.com/questions/19277244/fast-weighted-euclidean-distance-between-points-in-arrays/19285289#19285289 –

回答

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

給你你想要的點。

+0

其實這給了我一套真和假,但我認爲我瞭解你想做什麼,實際上是一個巨大的速度提升。 – api55

+0

@ api55:更新了答案。 –

+0

現在它工作正常,我的速度aprox大大增加。整個算法的10次嘗試從9.5到0.6,謝謝 – api55

0

如果你有一個非常大的點的集合,你可以計算出X &一個附加&減APLHA Y的範圍則消除一切從具體考慮B中把那邊界之外的點。