相关疑难解决方法(0)

在NumPy数组中概括切片操作

这个问题基于这个较老的问题:

给定一个数组:

In [122]: arr = np.array([[1, 3, 7], [4, 9, 8]]); arr
Out[122]: 
array([[1, 3, 7],
       [4, 9, 8]])
Run Code Online (Sandbox Code Playgroud)

鉴于其指数:

In [127]: np.indices(arr.shape)
Out[127]: 
array([[[0, 0, 0],
        [1, 1, 1]],

       [[0, 1, 2],
        [0, 1, 2]]])
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它们整齐地叠在一起形成一个新的2D阵列?这就是我想要的:

array([[0, 0, 1],
       [0, 1, 3],
       [0, 2, 7],
       [1, 0, 4],
       [1, 1, 9],
       [1, 2, 8]])
Run Code Online (Sandbox Code Playgroud)

Divakar的这个解决方案是我目前用于2D阵列的解决方案:

def indices_merged_arr(arr):
    m,n = arr.shape
    I,J = np.ogrid[:m,:n]
    out = np.empty((m,n,3), dtype=arr.dtype)
    out[...,0] = I
    out[...,1] = …
Run Code Online (Sandbox Code Playgroud)

python arrays indexing numpy

8
推荐指数
2
解决办法
456
查看次数

标签 统计

arrays ×1

indexing ×1

numpy ×1

python ×1