【问题标题】:Python Numpy Array geht values of neighbours邻居的Python Numpy Array geht值
【发布时间】:2019-05-12 10:48:37
【问题描述】:

我想获取 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)

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

的邻居值时
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])]

有人对此有解决方案吗?

谢谢!

【问题讨论】:

标签: python arrays list numpy neighbours


【解决方案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】:
# 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]

这将涵盖所有边缘情况。

【讨论】:

  • 我在您的代码中将“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 个邻居和一个自身)。
  • @Pablo 如果此解决方案有效,请接受答案。
  • 没错。但我的程序也需要其他人:P
  • @Pablo 我可以理解您有要求,但要解决问题,我们必须有一些模式。如果你想要这些数字是可以的,但对我来说问题是,基于你得出这些数字的逻辑。因为必须有一些逻辑或模式,所以我们可以将其应用于整个数组以使我们的结果一致。但是,如果我们谈论真正的邻居,那么这个解决方案就涵盖了这一点。如果您仍然想满足您的要求,您可以打开一个新问题,但请提及一些模式或逻辑来提取结果。
【解决方案3】:

我从一个伙伴那里得到了以下解决方案:

新数组:

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 的解决方案很感兴趣

【讨论】:

    猜你喜欢
    • 2014-12-14
    • 1970-01-01
    • 2021-09-04
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多