最近成员附加属性分析

nearest member aditional atribute analysis

我有以下数据框 df(sample):

         lat        lon  crs   Band1              x             y
0  41.855584  20.619156  b''  1568.0  468388.198606  4.633812e+06
1  41.855584  20.622590  b''  1562.0  468673.173031  4.633811e+06
2  41.855584  20.626023  b''  1605.0  468958.147443  4.633810e+06
3  41.859017  20.612290  b''  1598.0  467819.970900  4.634196e+06
4  41.859017  20.615723  b''  1593.0  468104.930108  4.634195e+06
5  41.859017  20.619156  b''  1600.0  468389.889303  4.634193e+06
6  41.859017  20.622590  b''  1586.0  468674.848486  4.634192e+06
7  41.859017  20.626023  b''  1577.0  468959.807656  4.634191e+06
8  41.859017  20.629456  b''  1584.0  469244.766814  4.634190e+06
9  41.859017  20.632889  b''  1598.0  469529.725959  4.634188e+06

字段xy是xy平面坐标,Band1是点高程(本质上是z坐标)。 Dataframe 是矩形网格,xy 作为中心网格坐标,Band1 作为网格高程。

我如何检测哪个网格单元格在 Band1 中相对于相邻单元格最高?

这种情况下的预期输出是数据框中的附加列,其布尔值定义该单元格在相邻 4 个单元格之前的海拔 Band1 最高。

我可以通过以下方式轻松获得相邻的网格距离和索引:

X=df[['x','y']].to_numpy()
nbrs = NearestNeighbors(n_neighbors=5, algorithm='ball_tree').fit(X)
distances, indices = nbrs.kneighbors(X)

带指数输出:

array([[0, 1, 5, 6, 4],
       [1, 2, 0, 6, 7],
       [2, 1, 7, 8, 6],
       [3, 4, 5, 0, 6],
       [4, 5, 3, 0, 6],
       [5, 6, 4, 0, 1],
       [6, 7, 5, 1, 2],
       [7, 8, 6, 2, 1],
       [8, 9, 7, 2, 6],
       [9, 8, 7, 2, 6]], dtype=int64)

我可以循环遍历数据框并比较所有成员,但由于我有 100 万行,它会消耗资源。 感谢任何帮助。

IIUC,可以用indices得到Band1列对应的值,然后用np.argmax参数axis设置为1得到最高的位置每行的值。如果值为 0,则表示该行的 Band1 高于邻居的 Band1,如:

df['local_high'] = np.argmax(df['Band1'].to_numpy()[indices], axis=1)==0

然后你得到

         lat        lon  crs   Band1              x          y  local_high
0  41.855584  20.619156  b''  1568.0  468388.198606  4633812.0       False
1  41.855584  20.622590  b''  1562.0  468673.173031  4633811.0       False
2  41.855584  20.626023  b''  1605.0  468958.147443  4633810.0        True
3  41.859017  20.612290  b''  1598.0  467819.970900  4634196.0       False
4  41.859017  20.615723  b''  1593.0  468104.930108  4634195.0       False
5  41.859017  20.619156  b''  1600.0  468389.889303  4634193.0        True
6  41.859017  20.622590  b''  1586.0  468674.848486  4634192.0       False
7  41.859017  20.626023  b''  1577.0  468959.807656  4634191.0       False
8  41.859017  20.629456  b''  1584.0  469244.766814  4634190.0       False
9  41.859017  20.632889  b''  1598.0  469529.725959  4634188.0       False