Numpy Crash Course


In [1]:
import numpy as np

Creating Arrays


In [2]:
# Create a Python list
my_list = [0, 1, 2, 3, 4]

In [3]:
# Transform Python list to numpy n-dimensional array
arr = np.array(my_list)

In [4]:
arr


Out[4]:
array([0, 1, 2, 3, 4])

In [5]:
type(arr)


Out[5]:
numpy.ndarray

In [6]:
# Create an array containing a sequence for given range
# By default the step is equal to 1.

np.arange(0, 10)


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

In [7]:
# last number is NOT INCLUDED!
# Step = 2
np.arange(start = 0, stop = 10, step = 2)


Out[7]:
array([0, 2, 4, 6, 8])

In [8]:
# Create a n-dimensional array of zeros
# In this scenario it will be array 5 by 5
np.zeros((5, 5))


Out[8]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

In [9]:
# Create an n-dimensional array of ones
# This array will have 2 rows and 4 columns
np.ones((2, 4))


Out[9]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.]])

In [10]:
# Generate a random integer between 
np.random.randint(0, 10)


Out[10]:
5

In [11]:
# Generate an n-dimensional array of random integers betwen 0 and (10-1) 
np.random.randint(0, 10, size = (3, 3))


Out[11]:
array([[1, 2, 6],
       [2, 2, 4],
       [1, 8, 4]])

In [12]:
# Create an n-dimensial array of size 'num'
# The number are evenly spaces (includin 'stop' values)
np.linspace(start = 0, stop = 10, num = 6, dtype = float)


Out[12]:
array([ 0.,  2.,  4.,  6.,  8., 10.])

In [13]:
np.linspace(0, 10, 101)


Out[13]:
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,
        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,
        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,
        4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,
        5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,
        6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,
        7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,  8.5,  8.6,  8.7,
        8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,  9.6,  9.7,  9.8,
        9.9, 10. ])

Operations


In [14]:
# Setting a specific seed for reproducability
np.random.seed(101)
arr = np.random.randint(0, 100, 10)

In [15]:
arr


Out[15]:
array([95, 11, 81, 70, 63, 87, 75,  9, 77, 40])

In [16]:
arr2 = np.random.randint(0, 100, 10)

In [17]:
arr2


Out[17]:
array([ 4, 63, 40, 60, 92, 64,  5, 12, 93, 40])

In [18]:
arr.max()


Out[18]:
95

In [19]:
arr.min()


Out[19]:
9

In [20]:
arr.mean()


Out[20]:
60.8

In [21]:
arr.argmin()


Out[21]:
7

In [22]:
arr.argmax()


Out[22]:
0

In [23]:
# Reshaping the array.
# The number of values before and after reshaping MUST BE EQUAL!
arr.reshape(2, 5)


Out[23]:
array([[95, 11, 81, 70, 63],
       [87, 75,  9, 77, 40]])

Indexing


In [24]:
mat = np.arange(0, 100).reshape(10, 10)

In [25]:
mat


Out[25]:
array([[ 0,  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, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
       [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
       [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

In [26]:
row = 0
col = 1

In [27]:
# Obtaining first value (of index = 0) from the second column (index = 1)
mat[row, col]


Out[27]:
1

In [28]:
# Slicing
# Obtaining all values from the second column (index  = 1)
mat[:, col]


Out[28]:
array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])

In [29]:
# Slicing
# Obtaining all values from the first row (index  = 0)
mat[row, :]


Out[29]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [30]:
mat[0:3, 0:3]


Out[30]:
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])

Masking


In [31]:
mat > 50


Out[31]:
array([[False, False, False, False, False, False, False, False, False,
        False],
       [False, False, False, False, False, False, False, False, False,
        False],
       [False, False, False, False, False, False, False, False, False,
        False],
       [False, False, False, False, False, False, False, False, False,
        False],
       [False, False, False, False, False, False, False, False, False,
        False],
       [False,  True,  True,  True,  True,  True,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True],
       [ True,  True,  True,  True,  True,  True,  True,  True,  True,
         True]])

In [32]:
mat[mat> 50]


Out[32]:
array([51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

Great Job!