我之前使用过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