用numpy中的零填充数组

时间:2022-01-28 21:27:53
h = numpy.zeros((2,2,2)) 

What is the last 2 for? Is it creating a multidimensional array or something?

最后2个是什么?它是在创建多维数组还是其他什么?

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.

我知道它被零填充,前两个值指定行和列,第三个是什么?先谢谢你。我试过谷歌,但我不能说出我的问题。

3 个解决方案

#1


8  

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数组)但是底层结构仍然是三维的。

#2


3  

Read (4,3,2) as: There's a building with 4 floors, each floor has 3 rows and 2 columns of rooms. Hence it is a 3-D array.

阅读(4,3,2):有一个4层楼的建筑,每层有3排2列房间。因此它是一个三维阵列。

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.]]])      

#3


1  

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"). Axis 0 has length a, axis 1 has length b, and axis 2 has length c.

如果数组的形状是(a,b,c),那么它具有NumPy用语3“轴”(或普通英语,3“维”)。轴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. The innermost bracket contains just a single item. Thus, the shape is (2, 2, 1).

最外面的括号包含2个项目,中间括号也包含2个项目。最里面的括号只包含一个项目。因此,形状为(2,2,1)。

#1


8  

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数组)但是底层结构仍然是三维的。

#2


3  

Read (4,3,2) as: There's a building with 4 floors, each floor has 3 rows and 2 columns of rooms. Hence it is a 3-D array.

阅读(4,3,2):有一个4层楼的建筑,每层有3排2列房间。因此它是一个三维阵列。

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.]]])      

#3


1  

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"). Axis 0 has length a, axis 1 has length b, and axis 2 has length c.

如果数组的形状是(a,b,c),那么它具有NumPy用语3“轴”(或普通英语,3“维”)。轴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. The innermost bracket contains just a single item. Thus, the shape is (2, 2, 1).

最外面的括号包含2个项目,中间括号也包含2个项目。最里面的括号只包含一个项目。因此,形状为(2,2,1)。