python - 将数组元素的邻居广义堆叠到 3D 数组中

标签 python arrays numpy vectorization

设置

给定一个 2D 数组,我想创建一个 3D 数组,其中沿第三维的值(即 stacked[row, col, :])是原始数组的展平邻居在 [行,列]。我想概括此过程以处理任意(但合理)的搜索半径。

前期研究

question看起来很有希望,但我不确定我是否可以在没有(几个)for 循环的情况下真正利用它的方法。为了简洁起见,我当前的方法应用了 1 的搜索半径。

还有这个question + answer很接近,但我正在专门寻找一种纯粹使用智能索引来避免循环的解决方案。

我现在拥有的

import numpy as np
np.random.seed(0)

x = np.random.random_integers(0, 10, size=(4, 5))
print(x)  # * highlights the neighbors we'll see later

[[ 5   0   3   3   7]
 [ 9  *3  *5  *2   4]
 [ 7  *6  *8  *8  10]
 [ 1  *6  *7  *7   8]]

# padding the edges 
padded = np.pad(x, mode='edge', pad_width=1) # pad_width -> search radius
print(padded)

[[ 5  5  0  3  3  7  7]
 [ 5  5  0  3  3  7  7]
 [ 9  9  3  5  2  4  4]
 [ 7  7  6  8  8 10 10]
 [ 1  1  6  7  7  8  8]
 [ 1  1  6  7  7  8  8]]

然后我们可以叠加所有的邻居。 这是我想概括的操作

blocked = np.dstack([
    padded[0:-2, 0:-2], # upper left
    padded[0:-2, 1:-1], # upper center
    padded[0:-2, 2:],   # upper right
    padded[1:-1, 0:-2], # middle left...
    padded[1:-1, 1:-1],
    padded[1:-1, 2:],
    padded[2:, 0:-2],   # lower left ...
    padded[2:, 1:-1],
    padded[2:, 2:],
])

如果单元格看起来像这样,则访问邻居(调用 reshape 仅用于说明目的)

print(blocked[2, 2, :].reshape(3, 3))
[[3 5 2]
 [6 8 8]
 [6 7 7]]

主要问题

对于给定的搜索半径,是否有一种有效的方法来泛化对 np.dstack 的调用?

最佳答案

这可能是一种方法-

import numpy as np

# Parameters
R = 3  # Radius
M1,N1 = padded.shape
rowlen = N1 - R + 1
collen = M1 - R + 1

# Linear indices for the starting R x R block
idx1 = np.arange(R)[:,None]*N1 + np.arange(R)

# Offset (from the starting block indices) linear indices for all the blocks
idx2 = np.arange(collen)[:,None]*N1 + np.arange(rowlen)

# Finally, get the linear indices for all blocks
all_idx = idx1.ravel()[None,None,:] + idx2[:,:,None]

# Index into padded for the final output
out = padded.ravel()[all_idx] 

这是半径的示例运行,R = 4 -

In [259]: padded
Out[259]: 
array([[ 5,  5,  0,  3,  3,  3],
       [ 5,  5,  0,  3,  3,  3],
       [ 7,  7,  9,  3,  5,  5],
       [ 2,  2,  4,  7,  6,  6],
       [ 8,  8,  8, 10,  1,  1],
       [ 6,  6,  7,  7,  8,  8],
       [ 6,  6,  7,  7,  8,  8]])

In [260]: out
Out[260]: 
array([[[ 5,  5,  0,  3,  5,  5,  0,  3,  7,  7,  9,  3,  2,  2,  4,  7],
        [ 5,  0,  3,  3,  5,  0,  3,  3,  7,  9,  3,  5,  2,  4,  7,  6],
        [ 0,  3,  3,  3,  0,  3,  3,  3,  9,  3,  5,  5,  4,  7,  6,  6]],

       [[ 5,  5,  0,  3,  7,  7,  9,  3,  2,  2,  4,  7,  8,  8,  8, 10],
        [ 5,  0,  3,  3,  7,  9,  3,  5,  2,  4,  7,  6,  8,  8, 10,  1],
        [ 0,  3,  3,  3,  9,  3,  5,  5,  4,  7,  6,  6,  8, 10,  1,  1]],

       [[ 7,  7,  9,  3,  2,  2,  4,  7,  8,  8,  8, 10,  6,  6,  7,  7],
        [ 7,  9,  3,  5,  2,  4,  7,  6,  8,  8, 10,  1,  6,  7,  7,  8],
        [ 9,  3,  5,  5,  4,  7,  6,  6,  8, 10,  1,  1,  7,  7,  8,  8]],

       [[ 2,  2,  4,  7,  8,  8,  8, 10,  6,  6,  7,  7,  6,  6,  7,  7],
        [ 2,  4,  7,  6,  8,  8, 10,  1,  6,  7,  7,  8,  6,  7,  7,  8],
        [ 4,  7,  6,  6,  8, 10,  1,  1,  7,  7,  8,  8,  7,  7,  8,  8]]])

关于python - 将数组元素的邻居广义堆叠到 3D 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29925684/

相关文章:

使用原始二进制数据对 np.ndarray 进行 Python3 管道 I/O 失败

python - 检查Python中的 float 是否正常

Python 递归函数 - 这里发生了什么?

python - pyzabbix Zabbix 发送器

python - Numpy 2D 数组沿特定轴由其他 2D 索引

python - 将 numpy 数组 block 转换为元组

python django 休息框架。如何以某种特定格式序列化外键 UUID?

c - 使用 malloc 创建二维和一维数组时出错

arrays - 如何在 Elm 中获取数组/列表中给定元素的第一个索引?

php - 使用单个表创建多维数组