NumPy 2D数组:选择圆形中的索引

dan*_*451 4 python arrays indexing numpy

对于某些矩形,我们可以非常有效地选择2D数组中的所有索引:

arr[y:y+height, x:x+width]
Run Code Online (Sandbox Code Playgroud)

...其中(x, y)是矩形和的左上角heightwidth矩形选择的高度(行数)和宽度(列数).

现在,假设我们想要选择位于给定中心坐标(cx, cy)和半径的特定圆中的2D阵列中的所有索引r.是否有一个numpy函数来有效地实现这一目标?

目前,我通过使用Python循环将索引添加到缓冲区(列表)中来手动预先计算索引.因此,对于大型2D阵列来说,这是非常无效的,因为我需要将位于某个圆圈中的每个整数排队.

# buffer for x & y indices
indices_x = list()
indices_y = list()

# lower and upper index range
x_lower, x_upper = int(max(cx-r, 0)), int(min(cx+r, arr.shape[1]-1))
y_lower, y_upper = int(max(cy-r, 0)), int(min(cy+r, arr.shape[0]-1))
range_x = range(x_lower, x_upper)
range_y = range(y_lower, y_upper)

# loop over all indices
for y, x in product(range_y, range_x):
    # check if point lies within radius r
    if (x-cx)**2 + (y-cy)**2 < r**2:
        indices_y.append(y)
        indices_x.append(x)

# circle indexing
arr[(indices_y, indices_x)]
Run Code Online (Sandbox Code Playgroud)

如上所述,对于较大的阵列/圆圈,此过程效率非常低.加快速度的任何想法?

如果有更好的方法来索引圆圈,这是否也适用于"任意"2D形状?例如,我可以以某种方式传递一个函数来表示任意形状的点的成员资格,以获得数组的相应numpy索引吗?

Chi*_*iel 6

您可以定义包含圆的蒙版.下面,我已经演示了一个圆圈,但你可以在mask赋值中编写任意函数.如果满足右侧的条件,则该字段mask具有尺寸arr并且具有值True,False否则.此掩码可以与索引运算符结合使用,以仅分配选择的索引,如线arr[mask] = 123.所示.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 32)
y = np.arange(0, 32)
arr = np.zeros((y.size, x.size))

cx = 12.
cy = 16.
r = 5.

# The two lines below could be merged, but I stored the mask
# for code clarity.
mask = (x[np.newaxis,:]-cx)**2 + (y[:,np.newaxis]-cy)**2 < r**2
arr[mask] = 123.

# This plot shows that only within the circle the value is set to 123.
plt.figure()
plt.pcolormesh(x, y, arr)
plt.colorbar()
plt.show()
Run Code Online (Sandbox Code Playgroud)

  • 辛苦了 我在计算机上对它进行计时,它的速度比我的方法快5倍,同时表明对于较大的数组,差异会变得更大(正如预期的那样,因为Python循环在大量迭代中变得更慢)。但是,您能详细解释一下mask =(x [np.newaxis,:]-cx)** 2 +(y [:,np.newaxis] -cy)** 2 &lt;r ** 2`吗?为什么我们确切需要/使用`np.arange`作为`x`和`y`作为初始值?顺便说一句:用`np.newaxis`这样的方法在添加后获得2D数组是一个很好的主意。 (2认同)