优化了二维numpy数组中的坐标选择



我有一个numpy坐标数组。我想选择Xmin和Xmax之间以及Ymin和Ymax之间的那些。

这是我的代码:

grid = np.random.randint(2, size=81).reshape(9,9)
List_of_coordinates = np.argwhere(grid == 0)
Xmin, Xmax, Ymin, Ymax = 0,3,6,9
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 1] >= Ymin]
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 1] < Ymax]
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 0] >= Xmin]
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 0] < Xmax]

有更快的路吗?

首先对网格进行切片,您将获得相对于切片开始的索引,然后将切片开始添加到绝对坐标:

List_of_coordinates = (np.argwhere(grid[Xmin:Xmax,Ymin:Ymax] == 0)
+np.array([Xmin,Ymin]))

示例

栅格:

array([[0, 1, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 1],
[1, 1, 0, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 0, 1, 0, 1, 0, 0]])

切片:

array([[0, 1, 1],
[1, 0, 0],
[0, 0, 1]])

0s的坐标:

array([[0, 6],
[1, 7],
[1, 8],
[2, 6],
[2, 7]])

您可以再次使用argwhere

import numpy as np
grid = np.random.randint(2, size=81).reshape(9,9)
List_of_coordinates = np.argwhere(grid == 0)
Xmin, Xmax, Ymin, Ymax = 0,3,6,9
# select coordinates between Xmin and Xmax and Ymin and Ymax
coordinates = np.argwhere(
np.logical_and(List_of_coordinates[:,0] >= Xmin, List_of_coordinates[:,0] <= Xmax) &
np.logical_and(List_of_coordinates[:,1] >= Ymin, List_of_coordinates[:,1] <= Ymax)
)

I(micro-(用更大的示例数据对您的解决方案、解决方案的优化版本和@mozway的方法(我没有包括@Mohammad的答案,因为输出不同,第一个结果与优化的原始结果大致相同(进行了基准测试。在两个核心的google colab实例上运行。

import numpy as np
grid = np.random.randint(2, size=(1000,1000))
Xmin, Xmax, Ymin, Ymax = 0,300,600,900
%%timeit
List_of_coordinates = np.argwhere(grid == 0)
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 1] >= Ymin]
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 1] < Ymax]
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 0] >= Xmin]
List_of_coordinates = List_of_coordinates[List_of_coordinates[:, 0] < Xmax]
List_of_coordinates

原始溶液10 loops, best of 5: 30.1 ms per loop

%%timeit
List_of_coordinates = np.argwhere(grid == 0)
LoC = List_of_coordinates[
(List_of_coordinates[:, 1] >= Ymin) &
(List_of_coordinates[:, 1] < Ymax) &
(List_of_coordinates[:, 0] >= Xmin) &
(List_of_coordinates[:, 0] < Xmax)               
]
LoC

优化的原始100 loops, best of 5: 19.4 ms per loop

%%timeit
LoC = np.argwhere(grid[Xmin:Xmax, Ymin:Ymax] == 0) + np.array([Xmin,Ymin])
LoC

@mozway的解决方案1000 loops, best of 5: 1.69 ms per loop

最新更新