Python Numpy数组的geht值

Python Numpy数组的geht值,python,arrays,list,numpy,neighbours,Python,Arrays,List,Numpy,Neighbours,我想得到np数组的所有邻域值 该数组看起来像: 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],

我想得到np数组的所有邻域值

该数组看起来像:

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]

这返回(我想要的)

但也有一些bug,比如当我需要

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

这给了我一系列的邻舍,但当我设置时,并没有给我所有的邻舍

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)
输出:
[11,12,15,16]

i, j = 3, 3                    
neighbors = find_neighbors(a, i, j)
neighbors
i, j = 2, 2                    
neighbors = find_neighbors(a, i, j)
neighbors
输出:
[6,7,8,10,11,12,14,15,16]

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

这将覆盖所有边缘情况

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

新阵列:

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]

这正是我所需要的,但我仍然对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()
这要求
i
j
为非负且小于
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])

你可以在这里找到一个解决方案:。我认为这个解决方案更像是python:。
I=3
j=3
的期望结果是什么?@VasilisG应该是“`[16,13,15,4,1,3,12,9,11]```@dome我看到了它们,但它们没有帮助我将“a”设置为“x”,我可以确认你的代码输出。非常感谢。但也许我不理解函数,但我需要'I=0 j=0'这个输出:`[1,2,4,5,6,8,13,14,16]`@Pablo
I=0,j=0
是第一个元素,它应该只有3个邻居。一个在右边->2,一个在底部->5,一个在对角线->6。因此,输出将包含4个元素(3个相邻元素和1个元素本身)。@Pablo如果此解决方案有效,请接受答案。没错。但是我的程序也需要其他的:P@Pablo我可以理解你有要求,但要解决问题,我们必须有一些模式。若你们想要那个些数字,这没关系,但我的问题是,你们根据什么逻辑推导出那个些数字。因为必须有一些逻辑或模式,所以我们可以将其应用于整个数组,以使结果一致。但如果我们谈论真正的邻居,那么这个解决方案就涵盖了这一点。如果你仍然想满足你的需求,你可以提出一个新问题,但请提及一些模式或逻辑来提取结果。嘿,谢谢你的解决方案!我回家后会看看这个的!