DEV Community

Chowdhury Sayeb Islam
Chowdhury Sayeb Islam

Posted on

Numpy Indexing and Selection

NumPy Indexing and Selection

In this lecture we will discuss how to select elements or groups of elements from an array.

import numpy as np

Enter fullscreen mode Exit fullscreen mode
#Creating sample array
arr = np.arange(0,11)

#Show
arr

Enter fullscreen mode Exit fullscreen mode

Bracket Indexing and Selection

The simplest way to pick one or some elements of an array looks very similar to python lists:

#Get values in a range
arr[1:5]

#Get values in a range
arr[0:5]
Enter fullscreen mode Exit fullscreen mode

Broadcasting

Numpy arrays differ from a normal Python list because of their ability to broadcast:

#Setting a value with index range (Broadcasting)
arr[0:5]=100

#Show
arr

Enter fullscreen mode Exit fullscreen mode
# Reset array, we'll see why I had to reset in  a moment
arr = np.arange(0,11)

#Show
arr
Enter fullscreen mode Exit fullscreen mode
#Important notes on Slices
slice_of_arr = arr[0:6]

#Show slice
slice_of_arr


Enter fullscreen mode Exit fullscreen mode
#Change Slice
slice_of_arr[:]=99

#Show Slice again
slice_of_arr
Enter fullscreen mode Exit fullscreen mode

Now notice that the changes also occur in our original array!

arr
Enter fullscreen mode Exit fullscreen mode

The Data is not copied, it's a view of the original array! This avoids memory problems!

#To get a copy, need to be explicit
arr_copy = arr.copy()

arr_copy
Enter fullscreen mode Exit fullscreen mode

Indexing a 2D array (matrices)

The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity.

arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))

#Show
arr_2d
Enter fullscreen mode Exit fullscreen mode
#Indexing row
arr_2d[1]

Enter fullscreen mode Exit fullscreen mode
# Format is arr_2d[row][col] or arr_2d[row,col]

# Getting individual element value
arr_2d[1][0]
Enter fullscreen mode Exit fullscreen mode
# Getting individual element value
arr_2d[1,0]
Enter fullscreen mode Exit fullscreen mode
# 2D array slicing

#Shape (2,2) from top right corner
arr_2d[:2,1:]
Enter fullscreen mode Exit fullscreen mode
#Shape bottom row
arr_2d[2]
Enter fullscreen mode Exit fullscreen mode
#Shape bottom row
arr_2d[2,:]
Enter fullscreen mode Exit fullscreen mode

Fancy Indexing

Fancy indexing allows one to select entire rows or columns out of order. To show this, let's quickly build out a NumPy Array:

#Set up matrix
arr2d = np.zeros((10,10))
Enter fullscreen mode Exit fullscreen mode
#Length of array
arr_length = arr2d.shape[1]
Enter fullscreen mode Exit fullscreen mode
#Set up array

for i in range(arr_length):
    arr2d[i] = i

arr2d
Enter fullscreen mode Exit fullscreen mode

Fancy indexing allows the following

arr2d[[2,4,6,8]]
Enter fullscreen mode Exit fullscreen mode
#Allows in any order
arr2d[[6,4,2,7]]
Enter fullscreen mode Exit fullscreen mode

More Indexing

Indexing a 2D Matrix can be a bit confusing at first, especially when you start to add in step size.

Selection

Let's briefly go over how to use brackets for selection based off of comparison operators.

arr = np.arange(1,11)
arr
Enter fullscreen mode Exit fullscreen mode
arr > 4
Enter fullscreen mode Exit fullscreen mode
bool_arr = arr>4
Enter fullscreen mode Exit fullscreen mode
bool_arr
Enter fullscreen mode Exit fullscreen mode
arr[bool_arr]
Enter fullscreen mode Exit fullscreen mode
arr[arr>2]
Enter fullscreen mode Exit fullscreen mode
x = 2
arr[arr>x]
Enter fullscreen mode Exit fullscreen mode

Enter fullscreen mode Exit fullscreen mode

Top comments (0)