Numpy 2d和可能的N-d索引由元组数组

时间:2014-12-15 04:18:54

标签: python arrays numpy indexing

使用1d数组,可以通过N-d整数数组对其进行索引,如下所示:

>>> rand = np.random.rand(9).astype(np.float32)
>>> rand
array([ 0.69786191,  0.09376735,  0.60141236,  0.35305005,  0.68340319,
        0.0746202 ,  0.11620298,  0.46607161,  0.90864712], dtype=float32)
>>> u = np.random.randint(0, 9, (2,2))
>>> u
array([[0, 6],
       [5, 6]])
>>> rand[u]
array([[ 0.69786191,  0.11620298],
       [ 0.0746202 ,  0.11620298]], dtype=float32)

但我不能对二维数组做同样的事情:

>>> rand2d = np.random.rand(9).astype(np.float32).reshape(3,3)
>>> rand2d
array([[ 0.83248657,  0.75025952,  0.87252802],
       [ 0.78049046,  0.92902303,  0.42035589],
       [ 0.80461669,  0.49386421,  0.56518084]], dtype=float32)
>>> u = np.random.randint(0, 3, (2,2,2))
>>> u
array([[[2, 2],
        [2, 2]],

       [[0, 2],
        [0, 1]]])
>>> rand2d[u]
array([[[[ 0.80461669,  0.49386421,  0.56518084],
         [ 0.80461669,  0.49386421,  0.56518084]],

        [[ 0.80461669,  0.49386421,  0.56518084],
         [ 0.80461669,  0.49386421,  0.56518084]]],

       [[[ 0.83248657,  0.75025952,  0.87252802],
         [ 0.80461669,  0.49386421,  0.56518084]],

        [[ 0.83248657,  0.75025952,  0.87252802],
         [ 0.78049046,  0.92902303,  0.42035589]]]], dtype=float32)

虽然我预期的结果是:

[[rand2d[2, 2], rand2d[2, 2]],
[rand2d[0, 2], rand2d[0, 1]]] ==
[[0.56518084, 0.56518084],
[0.87252802, 0.75025952]]

如何在不迭代的情况下实现这一目标?

3 个答案:

答案 0 :(得分:1)

直接来自example in the docs

>>> 
>>> x
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> 
>>> rows = np.array([[0,0],[3,3]])
>>> columns = np.array([[0,2],[0,2]])
>>> x[rows,columns]
array([[ 0,  2],
       [ 9, 11]])
>>> 

您可以看到它正在(0,0),(0,2)和(3,0),(3,2)中选择项目。

答案 1 :(得分:0)

您可以查看unravel_index。不确定这是否正是您所追求的,但它可能有用:

import numpy as np

rand2d = np.array([[ 0.83248657,  0.75025952,  0.87252802],
       [ 0.78049046,  0.92902303,  0.42035589],
       [ 0.80461669,  0.49386421,  0.56518084]], dtype=np.float32)


u = np.random.randint(0, 3, (2,2,2))

print(rand2d[np.unravel_index(u, rand2d.shape)])

示例输出是:

[[[ 0.87252802  0.75025952]
  [ 0.83248657  0.75025952]]

 [[ 0.87252802  0.87252802]
  [ 0.75025952  0.87252802]]]

答案 2 :(得分:0)

Thanx wwii。我的观点是使用每个元素坐标的矩阵索引数组(例如 - 对于每像素移位)。对于我的案例解决方案是:

>>> u
array([[[2, 2],
        [2, 2]],

       [[0, 2],
        [0, 1]]])
>>> ux = u.transpose(2,0,1)
>>> ux
array([[[2, 2],
        [0, 0]],

       [[2, 2],
        [2, 1]]])
>>> rand2d[ux[0], ux[1]]
array([[ 0.56518084,  0.56518084],
       [ 0.87252802,  0.75025952]], dtype=float32)

此外,我的解决方案是获取这个坐标数组,使用它并将其用于索引:

>>> ux = np.indices(rand2d.shape)
>>> ux
array([[[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]],

       [[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]]])
>>> u = ux.transpose(1,2,0)
>>> u
array([[[0, 0],
        [0, 1],
        [0, 2]],

       [[1, 0],
        [1, 1],
        [1, 2]],

       [[2, 0],
        [2, 1],
        [2, 2]]])
>>> u[1,1]-=1
>>> u
array([[[0, 0],
        [0, 1],
        [0, 2]],

       [[1, 0],
        [0, 0],
        [1, 2]],

       [[2, 0],
        [2, 1],
        [2, 2]]])
>>> ux = u.transpose(2,0,1) #Transpose back
>>> ux
array([[[0, 0, 0],
        [1, 0, 1],
        [2, 2, 2]],

       [[0, 1, 2],
        [0, 0, 2],
        [0, 1, 2]]])
>>> rand2d[ux[0], ux[1]]
array([[ 0.83248657,  0.75025952,  0.87252802],
       [ 0.78049046,  0.83248657,  0.42035589],
       [ 0.80461669,  0.49386421,  0.56518084]], dtype=float32)