Introduction

33

Indexing and Slicing are two of the most common operations that you need to be familiar with when working with Numpy arrays. You will use them when you would like to work with a subset of the array. This guide will take you through a little tour of the world of Indexing and Slicing on multi-dimensional arrays.

Let's talk about indexing a one-dimensional array. We have an array `array1`

:

`1 2 3`

`import numpy as np array1 = np.arange(0,10) array1`

python

**Output:**

`1`

`array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])`

python

Similar to programming languages like Java and C#, the index starts with zero. So to access the third element in the array, use the index 2.

`1`

`array1[2]`

python

**Output:**

`1`

`2`

python

Suppose I have a two-dimensional array:

`1 2`

`array1 = np.arange(12).reshape(4,3) array1`

python

**Output:**

`1 2 3 4`

`array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]])`

python

To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value ‘10,’ use the index ‘3’ for the row and index ‘1’ for the column.

`1`

`array1[3][1]`

python

**Output:**

`1`

`10`

python

Let’s go one level higher. To access a three-dimensional array, include the index for the third dimension as well. It may be difficult to imagine a three-dimensional array, but let’s try our best.

First, let me create a three-dimensional array:

`1 2`

`array1 = np.arange(18).reshape(3,2,3) array1`

python

**Output:**

`1 2 3 4 5 6 7 8`

`array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17]]])`

python

Note that there are three two-dimensional arrays of size two by three. If you reshape the array into size (5,3,4), there will be five two-dimensional arrays with a size of three by four.

So, to retrieve the value ‘13’, first go the third two-dimensional array by specifying the index ‘2.’ And once you find the desired two-dimensional array, access the element you need.

`1`

`array1[2][0][1]`

python

**Output:**

`1`

`13`

python

And you can extend the same concept to higher dimensional arrays.

To retrieve a single value, you use indexing. Similarly, to retrieve a collection of values, you would use slicing. For example, let me define a one-dimensional array

`1 2`

`array1 = np.arange(9) array1`

python

**Output:**

`1`

`array([0, 1, 2, 3, 4, 5, 6, 7, 8])`

python

This is the syntax to slice the array:

`1`

`array1[3:6]`

python

**Output:**

`1`

`array([3, 4, 5])`

python

Index ‘3’ represents the starting element of the slice and it's inclusive. Index ‘6’ represents the stopping element of the slice and it’s exclusive. That's the reason why we did not get the value ‘6’ in the output.

If you do not specify the starting and the stopping index you will get all the values.

`1`

`array1[:]`

python

**Output:**

`1`

`array([0, 1, 2, 3, 4, 5, 6, 7, 8])`

python

That's because if the indices are missing, by default, Numpy inserts the starting and stopping indices that select the entire array. So writing `array1[:]`

is equivalent to writing
`array1[0:9]`

You can extend this concept to include only the starting index. In this case, the slice includes all the elements from the starting index until the end of the array. For example:

`1`

`array1[4:]`

python

**Output:**

`1`

`array([4, 5, 6, 7, 8])`

python

Or, alternatively, specify only the stopping index.

`1`

`array1[:7]`

python

**Output:**

`1`

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

python

As expected, the slice includes all the elements from the start of the array until the indexed value. The rules for selecting the starting or the stopping element still hold true.

You can also use negative values for more flexibility. To understand how negative values work, take a look at this picture below:

Each element of an array can be referenced with two indices. For example, both ‘3’ and ‘-6’ can be used to retrieve the value ‘40.’ First let’s declare an array with similar values:

`1 2`

`array1 = np.array([10,20,30,40,50,60,70,80,90]) array1`

python

**Output:**

`1`

`array([10, 20, 30, 40, 50, 60, 70, 80, 90])`

python

Using both ‘3’ and ‘-6’ gives the same value.

`1`

`array1[3]`

python

**Output:**

`1`

`40`

python

`1`

`array1[-6]`

python

**Output:**

`1`

`40`

python

You can use this trick to slice the array as well. For example,

`1`

`array1[3:-2]`

python

**Output:**

`1`

`array([40, 50, 60, 70])`

python

And here is a visual representation of how it works:

Let’s try once more. This time let’s use a negative value for both the indices.

`1`

`array1[-3:-1]`

python

**Output:**

`1`

`array([70, 80])`

python

But note that you cannot change the order of the indices. In other words, the slice operation cannot travel backwards. If you try to do that, you will get an empty array as the output. For example:

`1`

`array1[-1:-3]`

python

**Output:**

`1`

`array([], dtype=int32)`

python

You can also include a `step`

index if you would like to skip a few elements in your slice operation. For example:

`1`

`array1[2:6:3]`

python

**Output:**

`1`

`array([30, 60])`

python

‘2:6’ indicate the index positions for the slice operation. The value ‘3’ indicates the slice operation to step three elements after every selection.

Or, you can do something like this as well:

`1`

`array1[:7:2]`

python

**Output:**

`1`

`array([10, 30, 50, 70])`

python

Here, ‘:7’ means slice from ‘0:7’ and the last value ‘2’ indicates a step operation to step two elements after every selection.

Let’s talk about slicing a two-dimensional array. I have this array `array1`

`1 2`

`array1 = np.arange(16).reshape(4,4) array1`

python

**Output:**

`1 2 3 4`

`array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])`

python

Slicing a two-dimensional array is very similar to slicing a one-dimensional array. You just use a comma to separate the row slice and the column slice. For example:

`1`

`array1[0:2, 1:3]`

python

**Output:**

`1 2`

`array([[1, 2], [5, 6]])`

python

Here is a pictorial representation:

Similarly, you can extend this for higher dimensional arrays.

Numpy Indexing and Slicing gives you powerful capabilities to select your data for further analysis. Understanding these basic operations will improve your skills in working with multidimensional arrays.

33