代码之家  ›  专栏  ›  技术社区  ›  Psychotechnopath

为什么3D纽比阵列是这样打印的?

  •  1
  • Psychotechnopath  · 技术社区  · 7 年前

    similar 但更多的是关于编程语言之间的差异,我仍然没有完全理解。我来解释一下。

    假设我要创建一个3行(长度)、5列(宽度)和2个深度的3D数组。所以一个3x5x2矩阵。

    import numpy as np
    a = np.zeros(30).reshape(3, 5, 2)
    

    [[[0. 0. 0. 0. 0.]  #We can still see three rows from top to bottom
      [0. 0. 0. 0. 0.]] #We can still see five columns from left to right
    
     [[0. 0. 0. 0. 0.]  #Depth values are shown underneath each other
      [0. 0. 0. 0. 0.]] 
    
     [[0. 0. 0. 0. 0.]
      [0. 0. 0. 0. 0.]]]
    

    但是,当我打印这个数组时,它会像这样打印:

    [[[0. 0.] #We can still see three rows from top to bottom,
      [0. 0.] #However columns now also appear from top to bottom instead of from left to right
      [0. 0.] #Depth values are now shown from left to right
      [0. 0.]
      [0. 0.]]
    
     [[0. 0.]
      [0. 0.]
      [0. 0.]
      [0. 0.]
      [0. 0.]]
    
     [[0. 0.]
      [0. 0.]
      [0. 0.]
      [0. 0.]
      [0. 0.]]]
    

    0 回复  |  直到 6 年前
        1
  •  0
  •   Psychotechnopath    6 年前

    将评论综合成一个恰当的答案:

    首先,看看 np.zeros(10).reshape(5, 2) . 这是5行2列,不是2行5列。前面加3表示5行2列的3个平面。你缺少的是你的新维度 前面 ,不是结尾。在数学中,通常在末尾加上额外的维数(比如用z扩展(x,y)变成(x,y,z)。然而,在计算机科学中,阵列尺寸通常是这样做的。它反映了数组在内存中通常以行主顺序存储的方式。