Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slicing 3d numpy arrays

Tags:

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!

like image 363
Patrick Rinker Avatar asked Jan 18 '15 14:01

Patrick Rinker


People also ask

Can you slice Numpy arrays?

You can slice a numpy array is a similar way to slicing a list - except you can do it in more than one dimension.

What is indexing and slicing in Numpy?

Numpy with Python Three types of indexing methods are available − field access, basic slicing and advanced indexing. Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function.

What is 3 dimensional Numpy array?

Introduction to NumPy 3D array. Arrays in NumPy are the data structures with high performance which are suitable for mathematical operations. The three levels of arrays nested inside one another represent the three-dimensional array in python, where each level represents one dimension.

How do you make a 3D array in Python?

In Python to initialize a 3-dimension array, we can easily use the np. array function for creating an array and once you will print the 'arr1' then the output will display a 3-dimensional array.


1 Answers

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]]
like image 87
unutbu Avatar answered Sep 30 '22 04:09

unutbu