Python Numpy数组的geht值

2024-05-23 14:16:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到一个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]

这是回报(我想要的)

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

但也有一些bug,比如当我需要的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])]

有人能解决这个问题吗?你知道吗

谢谢你!你知道吗


Tags: fordistnpexceptionneighbors数组findarray
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那样的解决方案感兴趣

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

这将涵盖所有的边缘情况。你知道吗

对于负索引,可以利用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])

相关问题 更多 >