Python 邻居的 Numpy 数组 geht 值

Python Numpy Array geht values of neighbours

我想获取 np.array 的所有邻居值。

数组看起来像:

x = np.array([  [1, 2, 3, 4 ],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 16] ])

我有的是:

i = 2
j = 2

n = x[i,j-1], x[i,j], x[i,j+1], x[i-1,j], x[i+1,j], x[i-1,j-1], x[i+1,j+1], x[i+1,j-1], x[i-1,j+1]

这个returns(我想要的)

(10, 11, 12, 7, 15, 6, 16, 14, 8)

但也有错误,例如当我想要

的邻居值时
i = 3
j = 3

这给出:

Exception has occurred: IndexError
index 4 is out of bounds for axis 1 with size 4

另一个灵魂是:

def find_neighbors(m, i, j, dist=1):
    return [row[max(0, j-dist):j+dist+1] for row in m[max(0,-1):i+dist+1]]

n = find_neighbors(x, i, j)

这给了我一个邻居数组,但当我设置

时也给了我不是所有的邻居
i = 0
j = 0

因为它只给我:

[array([1, 2]), array([5, 6])]

有人对此有解决方案吗?

谢谢!

# function to find the start row and column
def find_start(x):
    start = x-1 if x-1 >= 0 else 0
    return start

# function to find the end row and column
def find_end(x, shape):
    end = x+1 if x+1 <= shape else shape
    return end

def find_neighbors(a, i, j):
    neighbors = []
    row_start, row_end = find_start(i), find_end(i, a.shape[0])
    col_start, col_end = find_start(j), find_end(j, a.shape[1])

    for y in range(a.shape[0]):
        for z in range(a.shape[1]):
            if y >= row_start and y <= row_end:
                if z >= col_start and z <= col_end:
                    neighbors.append(a[y][z])
    return neighbors

i, j = 0, 0                    
neighbors = find_neighbors(a, i, j)
print(neighbors)

输出:[1, 2, 5, 6]

i, j = 3, 3                    
neighbors = find_neighbors(a, i, j)
neighbors

输出:[11, 12, 15, 16]

i, j = 2, 2                    
neighbors = find_neighbors(a, i, j)
neighbors

输出:[6, 7, 8, 10, 11, 12, 14, 15, 16]

这将涵盖所有边缘情况。

我从朋友那里得到了以下解决方案:

新数组:

homes = np.array([  [1, 2, 3, 4 ],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 16] ])

返回邻居值的代码:

neighbour  = []                                          
neighbour  += [homes[i][j]]                              # value itself
neighbour   += [homes[i][(j + 1) % n]]                   # value right 
neighbour  += [homes[i][(j - 1) % n]]                    # value left
neighbour  += [homes[(i + 1) % n][j]]                    # value down
neighbour  += [homes[(i + 1) % n][(j + 1) % n]]          # value right down
neighbour  += [homes[(i + 1) % n][(j - 1) % n]]          # value left down 
neighbour  += [homes[(i - 1) % n][j]]                    # vlaue up
neighbour  += [homes[(i - 1) % n][(j + 1) % n]]          # vlaue right up
neighbour  += [homes[(i - 1) % n][(j - 1) % n]]          # value left up 

哪个returns我:

i = 0
j = 0

[16, 13, 15, 4, 1, 3, 12, 9, 11]

这就是我需要的,但我仍然对 Abdur 的解决方案感兴趣

您可以利用 python 索引回绕来处理负索引。

def wrap_nb(x,i,j):
    return x[np.ix_(*((z-1, z, z+1-S) for z,S in zip((i,j), x.shape)))].ravel()

这要求 ij 为非负且小于 x 的形状。

如果不能保证:

def wrap_nb(x,i,j):
    return x[np.ix_(*(np.r_[z-1:z+2]%S for z,S in zip((i,j), x.shape)))].ravel()

示例:

>>> wrap_nb(x,1,-2)
array([ 2,  3,  4,  6,  7,  8, 10, 11, 12])
>>> wrap_nb(x,0,-1)
array([15, 16, 13,  3,  4,  1,  7,  8,  5])
>>> wrap_nb(x,0,0)
array([16, 13, 14,  4,  1,  2,  8,  5,  6])