代码之家  ›  专栏  ›  技术社区  ›  jlv

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

  •  6
  • jlv  · 技术社区  · 14 年前

    我想使用scipy.spatial的kdtree在二维数组中查找最近的邻居对(本质上是嵌套列表的维数为2的列表列表)。我生成列表列表,将其导入numpy的数组,然后创建kdree实例。然而,每当我试图对它运行“查询”时,我不可避免地会得到奇怪的答案。例如,当我键入:

    tree = KDTree(array)
    nearest = tree.query(np.array[1,1])
    

    最近的打印输出(0.0,0)。目前,我使用的数组基本上是范围(1,50)的y=x,所以我希望可以得到(1,1)的(2,2)的最近邻。

    我做错什么了,坐立不安的大师?

    编辑:或者,如果有人能给我指一个用于Python的kdtree包,他们已经用它搜索了一个给定点的最近邻居,我很乐意听到这个消息。

    1 回复  |  直到 14 年前
        1
  •  9
  •   dtlussier    14 年前

    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
    

    array y = x on the range [1,50]

    tree.query([1,1], k=2)