In [1]:
import numpy as np
In [2]:
#Creating sample array
arr = np.arange(0,11)
In [3]:
#Show
arr
Out[3]:
In [4]:
#Get a value at an index
arr[8]
Out[4]:
In [5]:
#Get values in a range
arr[1:5]
Out[5]:
In [6]:
#Get values in a range
arr[0:5]
Out[6]:
In [7]:
#Setting a value with index range (Broadcasting)
arr[0:5]=100
#Show
arr
Out[7]:
In [8]:
# Reset array, we'll see why I had to reset in a moment
arr = np.arange(0,11)
#Show
arr
Out[8]:
In [9]:
#Important notes on Slices
slice_of_arr = arr[0:6]
#Show slice
slice_of_arr
Out[9]:
In [10]:
#Change Slice
slice_of_arr[:]=99
#Show Slice again
slice_of_arr
Out[10]:
Now note the changes also occur in our original array!
In [11]:
arr
Out[11]:
Data is not copied, it's a view of the original array! This avoids memory problems!
In [12]:
#To get a copy, need to be explicit
arr_copy = arr.copy()
arr_copy
Out[12]:
In [13]:
arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
#Show
arr_2d
Out[13]:
In [14]:
#Indexing row
arr_2d[1]
Out[14]:
In [15]:
# Format is arr_2d[row][col] or arr_2d[row,col]
# Getting individual element value
arr_2d[1][0]
Out[15]:
In [16]:
# Getting individual element value
arr_2d[1,0]
Out[16]:
In [17]:
# 2D array slicing
#Shape (2,2) from top right corner
arr_2d[:2,1:]
Out[17]:
In [18]:
#Shape bottom row
arr_2d[2]
Out[18]:
In [19]:
#Shape bottom row
arr_2d[2,:]
Out[19]:
In [20]:
arr = np.arange(1,11)
arr
Out[20]:
In [21]:
arr > 4
Out[21]:
In [22]:
bool_arr = arr>4
In [23]:
bool_arr
Out[23]:
In [24]:
arr[bool_arr]
Out[24]:
In [25]:
arr[arr>2]
Out[25]:
In [26]:
x = 2
arr[arr>x]
Out[26]: