23

Consider the following:

A = np.zeros((2,3))
print(A)

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

This make sense to me. I'm telling numpy to make a 2x3 matrix, and that's 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.]]]

This doesn't make sense to me. Aren't I telling numpy to make a cube which has 4 2x3 matrices? I'm even more confused because although the data structure looks incorrect, the slicing works exactly as planned:

print(B[:,:,1])

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

I'm missing something about how these arrays are constructed, but I'm not sure what. Can someone explain what I'm missing or not understanding?

Thanks so much!

Patrick Rinker
  • 305
  • 1
  • 2
  • 10

3 Answers3

27

NumPy arrays iterate over the left-most axis first. Thus if B has shape (2,3,4), then B[0] has shape (3,4) and B[1] has shape (3,4). In this sense, you could think of B as 2 arrays of shape (3,4). You can sort of see the two arrays in the repr of 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 containing four 2x3 arrays by iterating over the last 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 could 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 this way. But as far as the repr of B is concerned, what you see corresponds to two (3x4) arrays since B iterates over the left-most axis first.

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]]
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
8

I hope the below example would clarify the second part of your question where you have asked about getting a 2X3 matrics 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, It means you have two matrices of size 3X4 as far as repr of B is concerned

Now in B[:,:,1] we are passing : , : and 1. First : indicates that we are selecting both the 3X4 matrices. The second : indicates that we are selecting all the rows from both the 3X4 matrices. The third parameter 1 indicates that we are selecting only the second column values of all the rows from both the 3X4 matrices. Hence we get

[[ 2  6 10]
 [14 18 22]]
Atul
  • 598
  • 5
  • 12
3

B is a 3D matrix. the indices that you specified (2x3x4) is exactly what is printed out. the outermost brackets have 2 elements, the middle brackets have 3 elements, and the innermost brackets have 4 elements.

adrianX
  • 619
  • 7
  • 21