Slicing 3d numpy arrays

Consider the following:

A = np.zeros((2,3)) print(A) [[ 0. 0. 0.] [ 0. 0. 0.]] 

That makes sense to me. I say numpy to make a 2x3 matrix, and what I get.

However, the following:

 B = np.zeros((2, 3, 4)) print(B) 

Gives me this:

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

That doesn't make sense to me. Am I not talking numpy about creating a cube with 4 2x3 matrices? I'm even more confused because, although the data structure does not look right, slicing works exactly as planned:

 print(B[:,:,1]) [[ 0. 0. 0.] [ 0. 0. 0.]] 

I am missing something about how these arrays are built, but I'm not sure what. Can someone explain what I am missing or do not understand?

Many thanks!

+9
source share
3 answers

NumPy arrays are first iterated over the leftmost axis. Thus, if B has the form (2,3,4), then B[0] has the form (3,4) and B[1] has the form (3,4). In this sense, you could think of B as 2 arrays of form (3,4). You can see two arrays in edition B :

 In [233]: B = np.arange(2*3*4).reshape((2,3,4)) array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], <-- first (3,4) array [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], <-- second (3,4) array [20, 21, 22, 23]]]) 

You can also think of B as four 2x3 arrays, iterating over the first index first:

 for i in range(4): print(B[:,:,i]) # [[ 0 4 8] # [12 16 20]] # [[ 1 5 9] # [13 17 21]] # [[ 2 6 10] # [14 18 22]] # [[ 3 7 11] # [15 19 23]] 

but you can just as easily think of B as three 2x4 arrays:

 for i in range(3): print(B[:,i,:]) # [[ 0 1 2 3] # [12 13 14 15]] # [[ 4 5 6 7] # [16 17 18 19]] # [[ 8 9 10 11] # [20 21 22 23]] 

NumPy arrays are completely flexible. But as for repr of B , what you see corresponds to two (3x4) arrays, since B iterates along the left-most axis.

 for arr in B: print(arr) # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] # [[12 13 14 15] # [16 17 18 19] # [20 21 22 23]] 
+12
source

B is a three-dimensional matrix. the indices you specified (2x3x4) are exactly what is being printed. the outermost brackets have 2 elements, the middle brackets have 3 elements, and the innermost brackets have 4 elements.

+1
source

I hope the example below clarifies the second part of your question, where did you ask about getting 2X3 matrices when you type print(B[:,:,1])

 import numpy as np B = [[[1,2,3,4], [5,6,7,8], [9,10,11,12]], [[13,14,15,16], [17,18,19,20], [21,22,23,24]]] B = np.array(B) print(B) print() print(B[:,:,1]) [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] [[ 2 6 10] [14 18 22]] 

Since the dimension of B is 2X3X4 , this means that you have two 3X4 matrices with regard to repr of B

Now in B[:,:,1] we go through : , : and 1 . First : indicates that we select both 3X4 matrices . Second : indicates that we select all rows from both 3X4 matrices . The third parameter 1 indicates that we select only the values ​​of the second column of all rows from both 3X4 matrices . From here we get

 [[ 2 6 10] [14 18 22]] 
0
source

Source: https://habr.com/ru/post/1211363/


All Articles