具有多维形状的np.zeros的结构

时间:2019-09-15 07:27:35

标签: python python-3.x numpy numpy-ndarray

我在github上的一个程序中看到了

img_out = np.zeros(((4,)+(512,1024,3)+(3,)))

我试图了解由此形成的numpy数组的结构。 documentation没有提供这种复杂形状的任何细节。有人可以解释一下我该如何解释该数组的结构。

3 个答案:

答案 0 :(得分:0)

@Sree的提及。

这是5D图像。

import numpy as np
a = np.zeros(((4,)+(512,1024,3)+(3,)))
a.shape

(4, 512, 1024, 3, 3) #output

512是图像的宽度

1024是高度

3是图像中的通道,例如rbg

所以仔细看一下代码,您将知道第一个元素和最后一个元素代表什么。

答案 1 :(得分:0)

您可以尝试理解一个更简单的5维示例:

print(np.zeros( (1, 2, 1, 2, 2)))

输出:

[[[[[0. 0.]
    [0. 0.]]]


  [[[0. 0.]
    [0. 0.]]]]]

在此示例中,您可以看到您拥有第5维的1个元素,其中包含第4维的2个元素,其中每个元素都包含第3维的1个元素,其中包含第2维的2个元素,它们每个都包含2个元素一维元素:

[<== your array 
    5th.1[<=== your 5th dimention, only 1 element of 5th dimention

        5th.1.4th.1[<=== your 4th dimention, 1st element of 4th dimention

            5th.1.4th.1.3th.1[<=== your 3rd dimention, only 1 element of  3rd dimention

                5th.1.4th.1.3th.1.2ed.1[<=== your 2ed dimention, 1st element of 2ed dimention,
                                             inside of this element are those elements of 1st dimention,
                                             there are 2 elements of 1st dimention:
                                                0.0, 0.0],

                 5th.1.4th.1.3th.1.2ed.2[<=== your 2ed dimention, 2end element of 2ed dimention,
                                              inside of this element are those elements of 1st dimention,
                                              there are 2 elements of 1st dimention:
                                                 0.0, 0.0]]], 



        5th.1.4th.2[<=== your 4th dimention, 2end element of 4th dimention

            5th.1.4th.2.3th.1[<=== your 3rd dimention, only 1 element of 3rd dimention

                5th.1.4th.2.3th.1.2ed.1[<=== your 2ed dimention, 1st element of  2ed dimention,
                                             inside of this element are those elements of 1st dimention,
                                             there are 2 element of 1st dimention:
                                                0.0, 0.0],

                 5th.1.4th.2.3th.1.2ed.2[<=== your 2ed dimention, 2end element of 2ed dimention,
                                              inside of this element are those elements of 1st dimention,
                                              there are 2 elements of 1st dimention:
                                                0.0, 0.0]]]]

为简单起见:

[1 X [2 x [1 x [[0.0, 0.0], 
                [0.0., 0.0]]]]]

您必须记住(4,)+(512,1024,3)+(3,) = (4, 512, 1024, 3, 3),他们使用3个元组来证明:

  1. 图像数(4),
  2. 高度(512),宽度(1024)和深度(3),
  3. 频道数(3)

与您的np.zeros(((4,)+(512,1024,3)+(3,)))示例类似,您可以简化为:

 [4 X [512 X [1024 X [[0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0]]]]]

答案 2 :(得分:0)

上面的代码可以像这样分解

shape = ((4,)+(512,1024,3)+(3,))
## above line is similar to joining list using +, and will result tuple (4,512,1024,3,3)


img_out = np.zeros(shape)
## so shape of img_out = (4,512,1024,3,3)