Pluralsight Logo
Author avatar

Ravikiran Srinivasulu

Author badge Author

Working with Numpy Arrays: Indexing & Slicing

Ravikiran Srinivasulu

Author BadgeAuthor
  • Oct 2, 2018
  • 7 Min read
  • 54 Views
  • Oct 2, 2018
  • 7 Min read
  • 54 Views
Python
Numpy

Introduction

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.

Indexing

Indexing a One-dimensional Array

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

Output:

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

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]

Output:

1
2

Indexing a Two-dimensional Array

Suppose I have a two-dimensional array:

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

Output:

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

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]

Output:

1
10

indexing

Indexing a Three-dimensional Array

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

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]]])

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.

For our case, you need to use the index 2, 0, and 1, where ‘0’ indicates the row 0 and ‘1’ indicates the column 1 within the third two-dimensional array.

1
array1[2][0][1]

Output:

1
13

index2dArray

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

Slicing

Slicing a One-dimensional Array

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

Output:

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

This is the syntax to slice the array:

1
array1[3:6]

Output:

1
array([3, 4, 5])

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[:]

Output:

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

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:]

Output:

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

Or, alternatively, specify only the stopping index.

1
array1[:7]

Output:

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

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.

Using Negative Indices

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

NegativeArray

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

Output:

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

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

1
array1[3]

Output:

1
40
1
array1[-6]

Output:

1
40

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

1
array1[3:-2]

Output:

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

And here is a visual representation of how it works:

NegativeValue

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

1
array1[-3:-1]

Output:

1
array([70, 80])

BothNegative

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]

Output:

1
array([], dtype=int32)

Using Step Index

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]

Output:

1
array([30, 60])

‘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]

Output:

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

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

Slicing a Two-dimensional Array

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

1
2
array1 = np.arange(16).reshape(4,4)
array1

Output:

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

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]

Output:

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

Here is a pictorial representation:

2darray

Similarly, you can extend this for higher dimensional arrays.

Conclusion

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.

4