Sandeep
Sandeep

Reputation: 131

How does slicing in three dimensional numpy array work?

import numpy as np
arr = np.array([[[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]]])
print(arr[0:2, : , 2] 

I know that elements 3, 6, 9 and 12 are selected but can't figure out whether the output is printed as a one-dimensional array or two dimensional array or more. How does it work?

Ouput:

enter image description here

Upvotes: 0

Views: 2460

Answers (5)

hpaulj
hpaulj

Reputation: 231355

Indexing/slicing a 3d array is really no different than doing the same on a 1d, 2d, or 20d.

In [580]: arr
Out[580]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
In [581]: arr[:,:,2]
Out[581]: 
array([[ 3,  6],
       [ 9, 12]])

For a (2,2,3) shape, slicing with 0:2 is the same as :, the whole dimension. So your example just selects the "2" subarray along the last axis. Indexing with a scalar reduces that dimension, so the (2,2,3) => (2,2).

Look where the result values are in the original - the right most column, but split across 2 planes. 2 high column in 2 plane results in a (2,2) array.

numpy has a flexible multidimensional array layout, and corresponding indexing system. That means arr[:,1,:] and arr[1,:,:] and even arr[1,:,2] all work the same way, though the results differ. But the underlying mechanism is basically the same.

In [582]: arr[1,:,:]
Out[582]: 
array([[ 7,  8,  9],
       [10, 11, 12]])
In [583]: arr[1,:,2]
Out[583]: array([ 9, 12])
In [585]: arr[:,1,2]
Out[585]: array([ 6, 12])

Upvotes: 0

Hex Heager
Hex Heager

Reputation: 139

Going In

Given the array:

    arr = np.array([[[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]]])

The shape of arr, given by arr.shape is (2,2,3).

That basically means that if we start from outside (and ignore the first square bracket), we have 2 arrays in that scope. If we enter to one of those we can count 2 arrays and if we enter either we find 3 elements. I think having this view is quite helpful in understanding the following.

Specifying arr[1,1,2] selects the second array(index 1) in the outermost scope, and then selects the second array in the following scope and then selects the third element. The output is a single number:

    12

specifying arr[:,1,2] first simultaneously selects all arrays at the outermost scope and then for each of these selects the second array (index 1). When we then enter into the next scope, we pick out the 3rd elements. This outputs two numbers:

    array([ 6, 12])

specifying arr[:, : , 2] outputs 4 numbers since 1. at the first scope we selected all arrays (2) 2. at the next scope we selected all array (2 for each in the first)

    array([[ 3,  6],
           [ 9, 12]])

Coming out

Instinctually, the reason why they appear as 2x2 array can be viewed as receding from the lowermost scope. Elements are getting enclosed in square brackets because that share a scope. 3 and 6 will be in one array while 9 and 12 will be in another array.

Upvotes: 1

Epsi95
Epsi95

Reputation: 9047

arr = np.array([[[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]]])

# array([[[ 1,  2,  3],
#         [ 4,  5,  6]],

#        [[ 7,  8,  9],
#         [10, 11, 12]]])

arr.shape # ------> (2,2,3)
# Think of them as axis
# lets create the first 2 axis of (`2`, ...)

#         |(1)
#         |
#         |
#         |         
#         |---------(0)


# now lets create second 2 axis of (2, `2`, ..)

#            (1,1)
#         |
#         |---(1,0)
#         |
#         |
#         |
#         |         |(0,1)
#         |---------|---(0,0)

# now lets create the last 3 axis of (2, 2, `3`)

#           /``(1,1,0) = 10
#          |-- (1,1,1) = 11
#         | \__(1,1,2) = 12
#         |
#         |  /``(1,0,0) = 7
#         |--|--(1,0,1) = 8
#         |  \__(1,0,2) = 9
#         |
#         |
#         |         /``(0,1,0) = 4
#         |         |--(0,1,1) = 5
#         |         \__(0,1,2) = 6
#         |         |
#         |         |
#         |---------|---/``(0,0,0) = 1
#                       |--(0,0,1) = 2
#                       \__(0,0,2) = 3

# now suppose you ask
arr[0, :, :] # give me the first axis alon with all it's component

#         |
#         |         /``(0,1,0) = 4
#         |         |--(0,1,1) = 5
#         |         \__(0,1,2) = 6
#         |         |
#         |         |
#         |---------|---/``(0,0,0) = 1
#                       |--(0,0,1) = 2
#                       \__(0,0,2) = 3

# So it will print 

# array([[1, 2, 3],
#        [4, 5, 6]])

arr[:, 0, :] # you ask take all the first axis 1ut give me only the first axis of the first axis and all its components

#           
#         
#         
#         
#         |  /``(1,0,0) = 7
#         |--|--(1,0,1) = 8
#         |  \__(1,0,2) = 9
#         |
#         |
#         |         
#         |         
#         |         
#         |         
#         |         
#         |---------|---/``(0,0,0) = 1
#                       |--(0,0,1) = 2
#                       \__(0,0,2) = 3

# so you get the output

# array([[1, 2, 3],
#        [7, 8, 9]])

# like wise you ask
print(arr[0:2, : , 2])
# so you are saying give (0,1) first axis, all of its children and only 3rd (index starts at 0 so 2 means 3) children
# 0:2 means 0 to 2 `excluding` 2; 0:5 means 0,1,2,3,4

#           
#          |
#         | \__(1,1,2) = 12
#         |
#         |  
#         |--
#         |  \__(1,0,2) = 9
#         |
#         |
#         |        
#         |         
#         |         \__(0,1,2) = 6
#         |         |
#         |         |
#         |---------|---/
#                       |
#                       \__(0,0,2) = 3

# so you get

# array([[ 3,  6],
#        [ 9, 12]])

Upvotes: 1

Numpy N-Dimensional Array/List Basically when using normal list slicing

Normal List

start,end=0,5
l=[1,2,3,4,5]
print(l[start:end])

[1,2,3,4,5]

Numpy Array ([List[list[list..]]])

So here when we refer the given numpy array arr=[[[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]]]

Level 0 splices the base Elems

arr[0:]  [[[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]]]
arr[1:]  [[[7,8,9],[10,11,12]]

Level 1 splices the child of base Elems

arr[0:,1:] [[[4,5,6]],[[10,11,12]]]

Level 2 splices the subchilds of child Elems

arr[0:,1:,1:] [[[5,6]],[[11,12]]]

Upvotes: 0

zoidberg
zoidberg

Reputation: 71

To check the shape of an array, just run the .shape method:

import numpy as np
arr = np.array([[[1,2,3], [4,5,6]], [[7,8,9],[10,11,12]]])
print(arr[0:2, : , 2].shape)

Output: (2, 2)

So you've got a 2 x 2 array.

Hope that's helpful!

Upvotes: 0

Related Questions