NumPy Array Indexing & Slicing Explained

In this article, we will learn about Numpy array indexing & slicing. First, I will cover how to index 1-dimensional arrays. And then, we will also learn how to index 2-dimensional arrays. Lastly, we will touch base on how to slice our Numpy arrays. 

If you are not familiar with Numpy arrays and how they work, then please make sure to check out the following article:

I have already covered the basics of Numpy and how to install it using the Anaconda distribution of Python. So make sure to go over that tutorial if you have to go over the basics. 

There is also a video version of this tutorial available. 

So, check out this course NumPy Data Science Essential Training from LinkedIn Learning.

Other than that, to follow this tutorial you will need basic knowledge of the Python programming language and an editor to run Numpy. 

I recommend following this tutorial using a Jupyter Notebook.

If you need a little refresher on Python then check out this cheat sheet for reference:

Indexing One-Dimensional NumPy Array

Let’s start by creating a 1-d array first. I am just going to use the arange() function to create one. 

import numpy as np 

arr = np.arange(0,11)
print(arr)

Output:

[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Now, to access the individual elements of our array, we will use their index positions. The cool part about this is that it is the same procedure as indexing a regular Python list. 

Let’s say we want the value at the index position 7. For that I can type:

arr[7]
print(arr[7])

The result is 7. Since the value at the index position 7 is actually a 7. But you get the idea. 

Related: NumPy Tutorial for Beginners – Arrays

Okay, you may have the question that what’s the difference between indexing a regular list and Numpy array indexing. The basic difference is that with a Numpy array we can use negative indexes to retrieve values starting from the end of the array. 

Here’s an array:

arr = np.arange(10,21)
print(arr)

Output:

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

To use the negative index, we will start from the last item in the array. The index -1 refers to the last item in our array. The index -2 will return the second last item. And it can go on and on. 

Say I want the third last item from our array. We can do:

arr[-3]

Output:

18

Now let’s try the same procedure with a regular Python list. 

myList = [1,2,3]
print(myList[-1])

Try this and you will get an error!

Negative indexing does not work for a regular Python list. But works for Numpy array indexing. 

Indexing Two-Dimensional NumPy Array

Let’s talk about indexing two-dimensional Numpy arrays. 

Why not we start by creating a 2-d Numpy array first. 

import numpy as np 

arr2d = ([1,2,3],[4,5,6])
arr2d = np.array(arr2d)
print(arr2d)

Output:

[[1, 2, 3], 
[4, 5, 6]]

 To grab elements from a 2-d array, we have to use a notation like this:

arr2d[1][2]

Output:

6

We are passing two index positions within two different square brackets. 

The first index position refers to the row number and the second index position refers to the column. 

In our case, we selected the value which is on the second row and third column. 

Remember, the first index position is the row and the second index position is the column. 

If you have any concerns regarding Numpy array indexing then don’t hesitate to comment below. 

Slicing One-Dimensional NumPy Arrays

Numpy array slicing works similar to slicing a regular Python list. 

First, let me show you how to slice 1-d arrays and then 2-d arrays.

Slicing a Numpy array involves the use of a colon operator. And the basic syntax for slicing looks like this:

data[from:to]

So, you have your data and you pass in two values separated by a colon. The first value is where the slicing starts. And the last value tells where the slicing should stop. 

Let me show you an example to clear things out. But first, let’s start by creating a 1-d Numpy array:

import numpy as np

arr = [10,11,12,13,14]
arr = np.array(arr)
print(arr)

Output:

[10, 11, 12, 13, 14]

I want to slice all the values starting from 11 till but not including14. To do that, I will type:

arr[1:4]

Output:

[11, 12, 13]

The process is similar to slicing a regular Python list. Although there is an index position of 14 during our slice procedure, the result does not include the value. 

Slicing Two-Dimensional Array

Let’s take a look at how we can slice a 2-d Numpy array. 

We have to create one first. 

import numpy as np

arr2d = ([[10,11,12],[13,14,15],[16,17,18]])
arr2d = np.array(arr2d)
print (arr2d)

Output:

[[10, 11, 12], 
[13, 14, 15],
[16, 17, 18]]

We want to slice a chunk from our array. To take or slice the chunk off, you already know that we have to use the slice notation a.k.a colon. 

So, if we want to grab the values from the top left corner of our array, we are targetting 

[[10,11]
[13,14]]

To achieve that I will type:

arr2d[0:2,0:2]

Output:

[[10, 11],
[13, 14]]

I know it is confusing. But try to understand what’s going on. I will break it down in a bit. 

So what’s going on here is that first, we are grabbing the first two rows by using the notation [:2] and then after the comma, we are asking Python to grab the first two columns only. 

This can be confusing for a lot of beginners so I want you to go over the syntax again to make sure that you completely understood. 

If I want to grab 14,15,17,18 then I will type:

arr2d[1:3,1:3]

Output:

[[14, 15], 
[17, 18]]

Think of it in this way. First, you slice the rows and then you slice the columns. 

Slicing can take time to master. If you have any confusion then go over the slicing part again. But I would not stress much about it since it just a matter of practice. The more you practice, the more you will get better at it. 

Conclusion

Numpy array indexing & slicing may take some practice to get comfortable. So, go over this tutorial as many times as you need to. Try with some different values and see what you can come up with.

Also, make sure to check out the course NumPy Data Science Essential Training to learn Numpy array indexing and slicing through interactive videos from LinkedIn Learning.

I highly recommend checking them out to get a better understanding.

Did you find any part of this Numpy array indexing & slicing tutorial confusing? If so. which part? Make sure to comment below.

Leave a Reply