【问题标题】:Scipy KDTree() Get rectagular shaped neighbouring grid pointsScipy KDTree() 获取矩形相邻网格点
【发布时间】:2017-05-10 01:23:14
【问题描述】:

我在使用这个模块时遇到了一个小问题。事实上,该模块正是我要求他做的事情......这是在这个网格中为给定坐标找到所有最近的网格点。

但是,当给定的坐标非常接近网格的一个点并且网格在一侧有较长的步长时,它会给出类似的结果:

所以在这张图片中,计算最近邻的点是你可以在左下角看到的红点。 KDTree 给出的结果是蓝色方块。绿色菱形是我想获得的第四个点,而不是图像顶部的蓝色菱形。

代码:

>>> grid.head()
          x         y
0  0.000000 -9.490125
1  0.959131 -9.490125
2  1.918263 -9.490125
3  2.877394 -9.490125
4  3.836526 -9.490125

>>> pt
[4.0092010999999998e-05, -9.4901299629261011]

>>>tree = ssp.KDTree(grid)
>>>dis, idx = tree.query(pt,4)

>>> idx 
array([  0,  71,   1, 142])

>>> grid.iloc[idx]
            x         y
0    0.000000 -9.490125
71   0.000000 -8.980481
1    0.959131 -9.490125
142  0.000000 -8.470837

问题:

有没有办法在查询中指定我们想要一个矩形数组?也许通过指定我们只希望一个 x 有 2 个 y?

【问题讨论】:

    标签: python scipy kdtree


    【解决方案1】:

    首先,让我们试试create a Minimal, Complete, and Verifiable example

    >>> import pandas as pd
    >>> import numpy as np
    >>> x0, dx = 0, 0.959131
    >>> x  = np.arange(x0, x0+5*dx,dx) 
    >>> y0, dy = -9.4901299629261011, 8.980481-8.470837
    >>> y  = np.arange(y0, y0+2*dy,dy)
    >>> data = np.transpose([np.tile(x, len(y)), np.repeat(y, len(x))])
    >>> grid = pd.DataFrame(data=data, columns=['x', 'y'])
    >>> grid.head()
              x        y
    0  0.000000 -9.49013
    1  0.959131 -9.49013
    2  1.918262 -9.49013
    3  2.877393 -9.49013
    4  3.836524 -9.49013
    

    其中grid.head() 是基于grid 的图形表示的数字等价物

    >>> grid
               x         y
    0   0.000000 -9.490130 # the red dot
    1   0.959131 -9.490130 # the bottom right blue square
    2   1.918262 -9.490130
    3   2.877393 -9.490130
    4   3.836524 -9.490130
    5   0.000000 -8.980486 # the middle left blue square
    6   0.959131 -8.980486 # the green diamond
    7   1.918262 -8.980486
    8   2.877393 -8.980486
    9   3.836524 -8.980486
    10  0.000000 -8.470842 # the unwanted top left blue square
    11  0.959131 -8.470842
    12  1.918262 -8.470842
    13  2.877393 -8.470842
    14  3.836524 -8.470842
    

    因此,您希望点 156 作为点 0 的邻域。

    为此,您可能需要查看the sklearn.neighbors module which implements the k-nearest neighbors algorithm 的函数kneighbors_graph。玩弄它,并设置Minkowski metricp的幂参数,大于2,比如说3(取p>2的想法基本上是为了减少欧几里得平方根2因子- - 在单位正方形的对角线和边之间--朝向1),如下

    >>> from sklearn.neighbors import kneighbors_graph
    >>> _3n_graph = kneighbors_graph(grid,
                                     n_neighbors=3,
                                     p=3,
                                     mode='connectivity',
                                     include_self=False)
    

    产量

    >>> grid.iloc[_3n_graph[0].indices]
              x         y
    5  0.000000 -8.980486
    1  0.959131 -9.490130
    6  0.959131 -8.980486
    

    【讨论】:

    • 天哪!很抱歉没有早点给你反馈。由于某些原因,我没有收到您回复的通知!很好的答案!最后(项目现在已经完成),如果可能的话,我采用了一种更“全局”的方法:我使用了scipy.interpolate.griddata,而不是实现我自己的基于网格插值数据的方式。附言对于非最小、完整和可验证的示例,我们深表歉意
    • @Alex 看来您实际上将矩形网格变成了正方形,不是吗?这样,即使使用欧几里得距离,你也能得到你想要的点。
    • 据我了解,scipy griddata 几乎可以将任何东西作为入口网格,所以我没有改变我所拥有的。
    • 我接受了你的回答,因为即使我修改了我的代码,它仍然回答了我提出的问题!再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多