邻居的Python Numpy Array geht值

时间:2019-05-12 10:48:37

标签: python arrays list numpy 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]

这返回(我想要的)

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

但是当我想要

的neightbour值时也出现了错误
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])]

有人对此有解决方案吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

# 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]

这将涵盖所有边缘情况。

答案 1 :(得分:1)

您可以利用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])

答案 2 :(得分:0)

我从伴侣那里得到了以下解决方案:

新数组:

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 

哪个还给我:

i = 0
j = 0

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

那是我所需要的,但我仍然对解决方案如阿卜杜勒的解决方案充满兴趣