简体   繁体   English

在numpy中用零填充数组

[英]Filling array with zeros in numpy

h = numpy.zeros((2,2,2)) 

What is the last 2 for?最后2个是什么? Is it creating a multidimensional array or something?它是创建一个多维数组还是什么?

Output:输出:

array([[[ 0.,  0.],
    [ 0.,  0.]],
   [[ 0.,  0.],
    [ 0.,  0.]]])

If it is creating number of copies, then what is happening when i do the following?如果它正在创建副本数,那么当我执行以下操作时会发生什么?

h = numpy.zeros((2,2,1))

Output:输出:

array([[[ 0.],
    [ 0.]],
   [[ 0.],
    [ 0.]]])

I understand that it is getting filled by zeros, and the first two values are specifying the row and column, what about the third?我知道它被零填充,前两个值指定行和列,第三个值呢? Thank you in advance.先感谢您。 And I tried Google, but I could not word my questions.我试过谷歌,但我无法说出我的问题。

by giving three arguments you're creating a three-dimensional array:通过提供三个参数,您正在创建一个三维数组:

numpy.array((2,2,2)) results in an array of size 2x2x2: numpy.array((2,2,2))生成大小为 2x2x2 的数组:

  0---0
 /   /|
0---0 0
|   |/
0---0

numpy.array((2,2,1)) results in an array of size 2x2x1: numpy.array((2,2,1))生成大小为 2x2x1 的数组:

0---0
|   |
0---0

numpy.array((2,1,2)) results in an array of size 2x2x1: numpy.array((2,1,2))生成大小为 2x2x1 的数组:

  0---0
 /   /
0---0

numpy.array((1,2,2)) results in an array of size 2x2x1: numpy.array((1,2,2))生成大小为 2x2x1 的数组:

  0
 /|
0 0
|/
0

in these representations the matrix "might look like numpy.array((2,2)) " (a 2x2 array) however the underlying structure is still three dimensional.在这些表示中,矩阵“可能看起来像numpy.array((2,2)) ”(一个 2x2 数组),但底层结构仍然是三维的。

Read (4,3,2) as: There's a building with 4 floors, each floor has 3 rows and 2 columns of rooms.(4,3,2)为:有一栋4层楼的建筑,每层有3行2列的房间。 Hence it is a 3-D array.因此它是一个 3-D 数组。

In [4]: np.zeros((4, 3, 2))                                                                      
Out[4]: 
array([[[ 0.,  0.],                                                                              
        [ 0.,  0.],                                                                              
        [ 0.,  0.]],                                                                             

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

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

       [[ 0.,  0.],                                                                              
        [ 0.,  0.],                                                                              
        [ 0.,  0.]]])      

The argument is specifying the shape of the array :参数指定数组形状

In [72]: import numpy as np

In [73]: h = np.zeros((2,2,2))

In [74]: h.shape
Out[74]: (2, 2, 2)

In [75]: h = np.zeros((2,2,1))

In [76]: h.shape
Out[76]: (2, 2, 1)

If the shape of an array is (a,b,c) , then it has in NumPy parlance 3 "axes" (or in common English, 3 "dimensions").如果数组的形状是(a,b,c) ,那么在 NumPy 的说法中它有 3 个“轴”(或在通用英语中,3 个“维度”)。 Axis 0 has length a , axis 1 has length b , and axis 2 has length c .轴 0 的长度为a ,轴 1 的长度为b ,轴 2 的长度为c


When you define h = np.zeros((2,2,1)) notice that the result has 3 levels of brackets:当您定义h = np.zeros((2,2,1))注意结果有 3 级括号:

In [77]: h
Out[77]: 
array([[[ 0.],
        [ 0.]],

       [[ 0.],
        [ 0.]]])

The outermost bracket contains 2 items, the middle brackets also contain 2 items each.最外面的括号包含 2 个项目,中间的括号每个也包含 2 个项目。 The innermost bracket contains just a single item.最里面的括号只包含一个项目。 Thus, the shape is (2, 2, 1).因此,形状是 (2, 2, 1)。

The last digit always means number of elements.最后一位数字始终表示元素数。 All the others are arrays or lists So, (3, 4, 3) means I need a list with 3 arrays each of the 3 containing 4 arrays, each of those 4 contains 3 elements .所有其他都是数组或列表 所以, (3, 4, 3) 意味着我需要一个包含 3 个数组的列表,其中 3 个数组中的每个数组包含 4 个数组,这 4 个中的每一个包含 3 个元素

{ [[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,0,0], [0,0,0], [0,0,0], [0,0,0]] }

2, 2, 1 means , list with 2 arrays each containing 2 arrays each with 1 element 2, 2, 1 表示 , list 有 2 个数组,每个数组包含 2 个数组,每个数组有 1 个元素

{ [[0], [0}],
[[0], [0]] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM