NumPy Indexing and Selection

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

In [1]:
import numpy as np

Creating sample array

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

Bracket Indexing and Selection

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

Get a Value at an Index

In [3]:
#Get a value at an index
arr[8]
Out[3]:
8

Get Values in a Range

In [4]:
#Get values in a range
arr[1:5]
Out[4]:
array([1, 2, 3, 4])
In [5]:
#Get values in a range
arr[0:5]
Out[5]:
array([0, 1, 2, 3, 4])

Broadcasting

Numpy arrays differ from a normal Python list because of their ability to broadcast i.e. set a value for a index range. You can't do this in normal Python list

Setting a value with index range (Broadcasting)

In [6]:
arr[0:5]=100

#Show
arr
Out[6]:
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])

Reset array, we'll see why I had to reset in a moment

In [7]:
arr = np.arange(0,11)

#Show
arr
Out[7]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

Important notes on Slices

In [8]:
slice_of_arr = arr[0:6]

#Show slice
slice_of_arr
Out[8]:
array([0, 1, 2, 3, 4, 5])

The bracket notation [:] means select everything

Change Slice

[:] means select eveything

In [9]:
slice_of_arr[:]=99

#Show Slice again
slice_of_arr
Out[9]:
array([99, 99, 99, 99, 99, 99])

Now note the changes also occur in our original array!

In [10]:
arr
Out[10]:
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!

Copying an Numpy Array

To get a copy, need to be explicit

In [11]:
arr_copy = arr.copy()

arr_copy
Out[11]:
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.

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

#Show
arr_2d
Out[12]:
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])

Indexing Row

In [13]:
arr_2d[1]
Out[13]:
array([20, 25, 30])

Format is arr_2d[row][col] or arr_2d[row,col]

Getting individual element value

In [14]:
arr_2d[1][0]
Out[14]:
20

Getting individual element value

In [15]:
arr_2d[1,0]
Out[15]:
20

2D Array Slicing

Shape (2,2) from top right corner

In [16]:
arr_2d[:2,1:]
Out[16]:
array([[10, 15],
       [25, 30]])

Shape bottom row

In [17]:
arr_2d[2]
Out[17]:
array([35, 40, 45])

Shape bottom row

In [18]:
arr_2d[2,:]
Out[18]:
array([35, 40, 45])

Conditional Selection

This is a very fundamental concept that will directly translate to pandas later on, make sure you understand this part!

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

In [19]:
arr = np.arange(1,11)
arr
Out[19]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [20]:
arr > 4
Out[20]:
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])
In [21]:
bool_arr = arr>4
bool_arr
Out[21]:
array([False, False, False, False,  True,  True,  True,  True,  True,
        True])

Return the array where boolean values are True

In [22]:
arr[bool_arr]
Out[22]:
array([ 5,  6,  7,  8,  9, 10])

The above 3 steps can be written as follows:

In [23]:
arr[arr>2]
Out[23]:
array([ 3,  4,  5,  6,  7,  8,  9, 10])
In [24]:
x = 2
arr[arr>x]
Out[24]:
array([ 3,  4,  5,  6,  7,  8,  9, 10])