【问题标题】:Stacking numpy arrays with padding用填充堆叠numpy数组
【发布时间】:2018-10-30 19:01:58
【问题描述】:

我有一个包含 32 个 numpy 数组的列表,每个数组都有(n, 108, 108, 2) 的形状,其中n 在每个数组中都不同。我想将它们全部堆叠起来以创建一个形状为(32, m, 108, 108, 2) 的numpy 数组,其中mns 中的最大值,较短的数组用零填充。

我该怎么做?

我昨天问过something similar,但在我的案例中使用深度数组时,那里的答案似乎中断了。

具体来说,我最终采用了这个解决方案,它产生了最干净的代码:

data = np.column_stack(zip_longest(*data, fillvalue=0))

但是现在它抛出了这个错误:

ValueError: setting an array element with a sequence.

【问题讨论】:

  • 你昨天得到了一些很好的答案,以及如何表达和测试这些想法的好例子。请按照这些示例,向我们展示您是如何尝试解决新问题的。我敢用minimal reproducible example 标记这个吗?
  • 一个快速的想法 - 您可以将数组重塑为 2d (n, 108*108*2),并应用其中一种 2d 解决方案。
  • 休息是什么意思?抛出异常,而不是想要的输出?
  • @hpaulj 我按照建议添加了更多细节
  • zip_longest 可以,但需要正确的fillvalue. Not an integer, but an array of the right shape (e.g. np.zeros((108,108,2)))

标签: python arrays numpy


【解决方案1】:

在我的情况下,我需要堆叠不同宽度的图像,并在左侧填充零。 对我来说这很好用:

np.random.seed(42)
image_batch = []
for i in np.random.randint(50,500,size=10):
image_batch.append(np.random.randn(32,i))
for im in image_batch:
    print(im.shape)

输出:(32, 152) (32, 485) (32, 398) (32, 320) (32, 156) (32, 121) (32, 238) (32, 70) (32, 152) (32, 171)

def stack_images_rows_with_pad(list_of_images):
    func = lambda x: np.array(list(zip_longest(*x, fillvalue=0))) # applied row wise
    return np.array(list(map(func, zip(*list_of_images)))).transpose(2,0,1)

res = stack_images_rows_with_pad(image_batch)

for im in rez:
    print(im.shape)

输出:(32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485)

【讨论】:

    【解决方案2】:

    我在this webpage找到了一个神乎其神的答案。

    pad_sequences 函数正是我所需要的。

    from tensorflow.python.keras.preprocessing.sequence import pad_sequences
    result = pad_sequences(imgs, padding='post')
    

    【讨论】:

      【解决方案3】:

      试试这个:

      # Create matrices with random first axis length.
      depth = np.random.randint(3,20,size=32)
      l = []
      lmax = 0
      for i in depth:
          l.append(np.ones((i,10,10,2)))
          lmax = i if i > lmax else lmax
      
      # Join the matrices:
      new_l = []
      for m in l:
          new_l.append(np.vstack([m, np.zeros((lmax-m.shape[0], 10, 10, 2))]))
      master = np.stack(new_l, axis=0)
      master.shape
      >>> (32, 19, 10, 10, 2)
      

      我发现np.pad 几乎不可能在更高维矩阵上使用 - 幸运的是,您所问的很简单,其中只有一个维度必须扩展,因此很容易使用 np.vstack 堆叠一个零使其符合新形状的数组。

      【讨论】:

        【解决方案4】:
        A = np.ones((4,3))
        
        
        border_top_bottom = np.zeros((A.shape[1])).reshape(1,A.shape[1])
        print(np.vstack([border_top_bottom,A,border_top_bottom]))
        
        temp = np.vstack([border_top_bottom,A,border_top_bottom])
        
        border_right_left = np.zeros((temp.shape[0])).reshape(temp.shape[0],1)
        print(np.hstack([np.hstack([border_right_left,temp,border_right_left])]))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-05-07
          • 1970-01-01
          • 2017-10-24
          • 2013-05-12
          • 2018-06-05
          • 2016-11-06
          • 2016-02-25
          相关资源
          最近更新 更多