使用scipy.spatial时的数据类型问题

6
我想使用scipy.spatial的KDTree在二维数组(本质上是嵌套列表,嵌套列表的维度为2)中找到最近邻对。我生成了我的嵌套列表,并将其传输到numpy的数组中,然后创建了KDTree实例。然而,每当我尝试在其上运行“query”时,我总是得到奇怪的答案。例如,当我输入:
tree = KDTree(array)
nearest = tree.query(np.array[1,1])

nearest打印出(0.0, 0)。目前,我正在使用一个基本上是y = x的数组,范围为(1,50),所以我期望对于(1,1),我应该得到(2,2)的最近邻居。

我做错了什么,scipy大师们?

编辑: 或者,如果有人能向我指出一个他们用于给定点的最近邻搜索的python KDTree软件包,我会很乐意听取建议。

1个回答

9
我之前使用过 scipy.spatial,相较于 scikits.ann,它在接口方面有很大的改进,看起来非常不错。
在这种情况下,我认为你可能会混淆 tree.query(...) 调用的返回值。根据 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]上。 scipy.spatial.KDTree.query函数有许多其他选项,因此,如果您想确保获取最近的邻居而不是自己,请尝试:
tree.query([1,1], k=2)

这将返回两个最近的邻居,你可以进一步应用逻辑,使得当距离为零时(即查询点是用于构建树的数据项之一),选择第二个最近的邻居而不是第一个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接