我之前使用過scipy.spatial
,與scikits.ann
相比,它似乎是一個不錯的改進(特別是接口)。
在這種情況下,我認爲你已經混淆了你的tree.query(...)
呼叫的回報。從scipy.spatial.KDTree.query
docs:
Returns
-------
d : array of floats
The distances to the nearest neighbors.
If x has shape tuple+(self.m,), then d has shape tuple if
k is one, or tuple+(k,) if k is larger than one. Missing
neighbors are indicated with infinite distances. If k is None,
then d is an object array of shape tuple, containing lists
of distances. In either case the hits are sorted by distance
(nearest first).
i : array of integers
The locations of the neighbors in self.data. i is the same
shape as d.
因此,在這種情況下,當您查詢就近[1,1]
給你:
distance to nearest: 0.0
index of nearest in original array: 0
這意味着[1,1]
是你的原始數據的第一行array
,預計給您的數據是y = x on the range [1,50]
。
的scipy.spatial.KDTree.query
功能有很多其他選擇,所以例如,如果你想確保獲得近鄰本身不是嘗試:
tree.query([1,1], k=2)
這將返回最近的鄰居,您可以應用更多的邏輯,以便在返回距離爲零的情況下(即查詢的點是用於構建樹的數據項之一),將採用第二個最近的鄰居而不是第一個。
非常感謝。現在更有意義! – jlv 2010-10-06 19:39:46