In this lecture we will discuss how to select elements or groups of elements from an array.
import numpy as np
Creating sample array
arr = np.arange(0,11)
arr
#Get a value at an index
arr[8]
#Get values in a range
arr[1:5]
#Get values in a range
arr[0:5]
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
arr[0:5]=100
#Show
arr
Reset array, we'll see why I had to reset in a moment
arr = np.arange(0,11)
#Show
arr
slice_of_arr = arr[0:6]
#Show slice
slice_of_arr
The bracket notation
[:]
means select everything
Change Slice
[:]
means select eveything
slice_of_arr[:]=99
#Show Slice again
slice_of_arr
Now note the changes also occur in our original array!
arr
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
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
arr_2d[1]
Format is arr_2d[row][col]
or arr_2d[row,col]
arr_2d[1][0]
arr_2d[1,0]
Shape (2,2) from top right corner
arr_2d[:2,1:]
Shape bottom row
arr_2d[2]
Shape bottom row
arr_2d[2,:]
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.
arr = np.arange(1,11)
arr
arr > 4
bool_arr = arr>4
bool_arr
Return the array where boolean values are True
arr[bool_arr]
The above 3 steps can be written as follows:
arr[arr>2]
x = 2
arr[arr>x]