sliced pepperoni pizza on white ceramic plate

Mastering Array Slicing: Unleashing the Power of Indexing




After reading this post, you should be able to mastering array slicing. Many don’t understand what is slicing, you can consider slicing as select elements by index in an array.

(All experiments are done using python with NumPy)

First we need to know what’s the meaning of [:] or [::], what you might already seen in many posts. Let’s begin with slicing a 1-D array.

Figure 1: Index of 1-D array

Slicing 1-D array

First, let’s define an 1-D array a using numpy, and do some slicing:

Python
>>> import numpy as np
>>> a = np.array([0, 1, 2, 3, 4])

>>> a[:3]
array([0, 1, 2])

>>> a[3:]
array([3, 4])

>>> a[1:4]
array([1, 2, 3])

As we can see the output, it begin with index 1 and stop at index 4 (which are not included), so we can guess the param before : means the start index and the param after : means the end index. And if you leave them empty, start default to 0 and end default to the length(a). You can also see Figure 2 to understand visualy.

Figure 2: Slice of 1-D array

Great! Let’s try slice more using [start:end]. How can we have array([2, 3, 4])?

Slicing 1-D array with step

Now, let’s move on adding :step after the start:end, which now becomes [start:end:step]. What it does is define a step length which default to 1 if leave empty. Also shown in Figure 3.

Python
>>> a[0:5:2]
array([0, 2, 4])

Figure 3: Slice of 1-D array with step defined

Negative Slicing

Python
>>> import numpy as np
>>> a = np.array([0,1,2,3,4])

>>> a[-3:-1]
array([2, 3])

>>> a[::-1]
array([4, 3, 2, 1, 0])

You can see as Figure 4 shown below. If define a negative step, it means move the index from left to right. While a negative step is defined, start index default to -1 and end index default to -length(a)-1 (the position right before index 0, i.e. index -5. So we can reach the element at index 0/-5)

Figure 4: Slice of 1-D array with negative index or step

Slicing 2-D array

Let’s define a new 2-D array b using numpy, and do some slicing:

Python
>>> import numpy as np
>>> b = np.array([[ 0,  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[1, 1:3]
array([6, 7])

>>> b[:, 2]
array([ 2,  7, 12, 17, 22])

>>> b[2:4, 2:]
array([[12, 13, 14],
       [17, 18, 19]])
       
>>> b[::2, ::3]
array([[ 0,  3],
       [10, 13],
       [20, 23]])

Visualization shown in Figure 5 below.

Figure 5: Slice of 2-D array

Slicing 3-D array

First, let’s see how the three axis were ordered in Figure 6.

Figure 6: Axis of 3-D array

Now, here are some examples slicing a 3-D array c, also shown in Figure 7:

Python
>>> import numpy as np
>>> c = np.array([[[ 0,  1,  2,  3,  4],
                   [ 5,  6,  7,  8,  9],
                   [10, 11, 12, 13, 14],
                   [15, 16, 17, 18, 19],
                   [20, 21, 22, 23, 24]],
                  [[25, 26, 27, 28, 29],
                   [30, 31, 32, 33, 34],
                   [35, 36, 37, 38, 39],
                   [40, 41, 42, 43, 44],
                   [45, 46, 47, 48, 49]],
                  [[50, 51, 52, 53, 54],
                   [55, 56, 57, 58, 59],
                   [60, 61, 62, 63, 64],
                   [65, 66, 67, 68, 69],
                   [70, 71, 72, 73, 74]],
                  [[75, 76, 77, 78, 79],
                   [80, 81, 82, 83, 84],
                   [85, 86, 87, 88, 89],
                   [90, 91, 92, 93, 94],
                   [95, 96, 97, 98, 99]]])

>>> c[1, 2, :3]
array([35, 36, 37])

>>> c[::2, ::2, ::]
array([[[ 0,  1,  2,  3,  4],
        [10, 11, 12, 13, 14],
        [20, 21, 22, 23, 24]],

       [[50, 51, 52, 53, 54],
        [60, 61, 62, 63, 64],
        [70, 71, 72, 73, 74]]])

>>> c[2:4, 3:, 3:]
array([[[68, 69],
        [73, 74]],

       [[93, 94],
        [98, 99]]])

Figure 7: Slice of 3-D array

Congratulations! You’ve learned how to slicing array in most cases. Now give it a try slicing your own arrays!



Leave a Reply

Your email address will not be published. Required fields are marked *