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

arr = np.arange(0,11)

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 = np.arange(0,11)
arr[0:5]
>> array([0, 1, 2, 3, 4])

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
>> array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])

#Important notes on Slices
arr = np.arange(0,11)
slice_of_arr = arr[0:6]
>> array([0, 1, 2, 3, 4, 5])

#Change Slice
slice_of_arr[:]=99
>> array([99, 99, 99, 99, 99, 99])

Now note the changes also occur in our original array!

arr
>> array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

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

To copy data:

#To get a copy, need to be explicit
arr_copy = arr.copy()
>> array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

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]))
>> array([[ 5, 10, 15],
[20, 25, 30],
[35, 40, 45]])

#Indexing row
arr_2d[1]
>> array([20, 25, 30])

# Format is arr_2d[row][col] or arr_2d[row,col]
# Getting individual element value
arr_2d[1][0]
arr_2d[1,0]
>> 20

# 2D array slicing
#Shape (2,2) from top right corner
arr_2d[:2,1:]
>> array([[10, 15],
[25, 30]])

Fancy Indexing

Fancy indexing allows you to select entire rows or columns out of order,to show this, let’s quickly build out a numpy array:

#Set up array
arr = np.arange(1,26)
arr.reshape(5,5)
>> array([[ 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]])

Fancy indexing allows the following

arr2d[[2,4,6,8]]
>> array([2, 4, 6, 8])

Selection

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

arr = np.arange(1,11)
arr
>> array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

arr > 4
>> array([False, False, False, False,  True,  True,  True,  True,  True,  True], dtype=bool)

bool_arr = arr>4
bool_arr
>> array([False, False, False, False,  True,  True,  True,  True,  True,  True], dtype=bool)

arr[bool_arr]
>> array([ 5,  6,  7,  8,  9, 10])

arr[arr>2]
>> array([ 3,  4,  5,  6,  7,  8,  9, 10])

x = 2
arr[arr>x]
>> array([ 3,  4,  5,  6,  7,  8,  9, 10])