Filling an array with zeros in numpy

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

What are the last 2 for? Does it create a multidimensional array or something else?

Output:

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

If it creates the number of copies, then what happens when I do the following?

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

Output:

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

I understand that it is filled with zeros, and the first two values ​​indicate the row and column, but what about the third? Thank you in advance. And I tried Google, but I could not answer my questions.

+6
source share
3 answers

specifying three arguments that you create a three-dimensional array:

numpy.array((2,2,2)) leads to an array of size 2x2x2:

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

numpy.array((2,2,1)) leads to an array of size 2x2x1:

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

numpy.array((2,1,2)) leads to an array of size 2x2x1:

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

numpy.array((1,2,2)) leads to an array of size 2x2x1:

  0 /| 0 0 |/ 0 

in these representations, the matrix "may look like numpy.array((2,2)) " (2x2 array), however, the basic structure is still three-dimensional.

+8
source

Read (4,3,2) how: There is a building with 4 floors, each floor has 3 rows and 2 columns of rooms. Therefore, it is a three-dimensional array.

 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
source

The argument indicates 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 the array is (a,b,c) , then it has an expression of 3 "axes" in NumPy (or in general English, 3 "dimensions"). Axis 0 has a length a , axis 1 has a length b , and axis 2 has a length c .


When you define h = np.zeros((2,2,1)) , note that the result has 3 levels of brackets:

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

The outermost bracket contains 2 elements, the middle brackets also contain 2 elements. The innermost bracket contains only one element. Thus, the form is (2, 2, 1).

+1
source

All Articles