【问题标题】:How to mix numpy slices to list of indices?如何将 numpy 切片混合到索引列表中?
【发布时间】:2016-08-02 09:17:51
【问题描述】:

我有一个numpy.array,叫grid,形状:

grid.shape = [N, M_1, M_2, ..., M_N]

N、M_1、M_2、...、M_N的值只有在初始化后才知道。

对于此示例,假设 N=3 且 M_1 = 20、M_2 = 17、M_3 = 9:

grid = np.arange(3*20*17*9).reshape(3, 20, 17, 9)

我正在尝试循环遍历这个数组,如下:

for indices, val in np.ndenumerate(grid[0]):
    print indices
    _some_func_with_N_arguments(*grid[:, indices])

在第一次迭代时,索引 = (0, 0, 0) 并且:

grid[:, indices] # array with shape 3,3,17,9

而我希望它是一个仅包含三个元素的数组,就像:

grid[:, indices[0], indices[1], indices[2]] # array([   0, 3060, 6120])

但是我不能像上一行那样实现,因为我不知道先验indices 的长度是多少。

我使用的是 python 2.7,但欢迎使用与版本无关的实现 :-)

【问题讨论】:

    标签: python arrays numpy indexing slice


    【解决方案1】:

    我相信您正在寻找的是grid[1:][grid[0]]

    grid = np.array([
            [0, 2, 1],  # N
            [1, 9, 3, 6],  # M_1
            [7, 8, 2, 5, 0, 8, 3],  # M_2
            [4, 8]  # M_3
        ])
    
    np.array([grid[a[0] + 1][n] for a, n in np.ndenumerate(grid[0])])
    # array([1, 2, 8])
    

    【讨论】:

    • 我认为这不能回答我的问题。我想访问一个标量,而您的输出返回一个数组!
    • 啊。我一定误解了这个问题。您想从数组中提取哪个值?
    • 亲爱的@2Cubed,请看一下我的工作示例。
    • 啊,我明白了。我相信我更新的回复应该是您正在寻找的。 (这是我能找到的最干净的方法,长度不一的 M_N。)
    【解决方案2】:

    您可以手动将slice(None) 添加到索引元组中:

    >>> grid.shape
    (3, 20, 17, 9)
    >>> indices
    (19, 16, 8)
    >>> grid[:,19,16,8]
    array([3059, 6119, 9179])
    >>> grid[(slice(None),) + indices]
    array([3059, 6119, 9179])
    

    有关更多信息,请参阅文档中的 here

    【讨论】:

      【解决方案3】:

      我想你想要这样的东西:

      In [134]: x=np.arange(24).reshape(4,3,2)
      
      In [135]: x
      Out[135]: 
      array([[[ 0,  1],
              [ 2,  3],
              [ 4,  5]],
      
             [[ 6,  7],
              [ 8,  9],
              [10, 11]],
      
             [[12, 13],
              [14, 15],
              [16, 17]],
      
             [[18, 19],
              [20, 21],
              [22, 23]]])
      
      In [136]: for i,j in np.ndindex(x[0].shape):
           ...:     print(i,j,x[:,i,j])
           ...:     
      (0, 0, array([ 0,  6, 12, 18]))
      (0, 1, array([ 1,  7, 13, 19]))
      (1, 0, array([ 2,  8, 14, 20]))
      (1, 1, array([ 3,  9, 15, 21]))
      (2, 0, array([ 4, 10, 16, 22]))
      (2, 1, array([ 5, 11, 17, 23]))
      

      第一行在哪里:

      In [142]: x[:,0,0]
      Out[142]: array([ 0,  6, 12, 18])
      

      将索引元组解压缩为i,j 并在x[:,i,j] 中使用它是执行此索引的最简单方法。但要将其推广到其他数量的维度,我将不得不使用一些元组。 x[i,j]x[(i,j)] 相同。

      In [147]: for ind in np.ndindex(x.shape[1:]):
           ...:     print(ind,x[(slice(None),)+ind])
           ...:     
      ((0, 0), array([ 0,  6, 12, 18]))
      ((0, 1), array([ 1,  7, 13, 19]))
      ...
      

      enumerate:

      for ind,val in np.ndenumerate(x[0]):
          print(ind,x[(slice(None),)+ind])
      

      【讨论】:

      • 亲爱的@hpaulj,感谢您的回答。我接受了较早的 DSM。
      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      相关资源
      最近更新 更多