我用过scipy.spatial
与之前相比,这似乎是一个不错的改进(尤其是界面)scikits.ann
.
在这种情况下,我认为你混淆了你的回报tree.query(...)
称呼。来自scipy.spatial.KDTree.query
docs http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.KDTree.query.html#scipy.spatial.KDTree.query:
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]
.
The scipy.spatial.KDTree.query
函数有很多其他选项,因此,例如,如果您想确保获得不是其本身的最近邻居,请尝试:
tree.query([1,1], k=2)
这将返回two最近邻,您可以应用进一步的逻辑,在返回距离为零的情况下(即查询的点是用于构建树的数据项之一),将采用第二个最近邻而不是第一个。