Numpy Basics


In [2]:
import numpy as np      # stands for numerical python

In [3]:
# Numpy array comes in two flavors namely vectors and matrices
# vectors are 1-dimensional arrays
# matrices are multi-dimensional arrays

Vectors vs Matrices


In [4]:
# Creating a numpy array
my_list = [1,2,3]
x = np.array(my_list)
type(x)


Out[4]:
numpy.ndarray

In [5]:
x


Out[5]:
array([1, 2, 3])

In [6]:
my_matrix = [[1,2,3],[4,5,6], [7,8,9]]
np.array(my_matrix)


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

arange, zeros, ones, eye and linspace


In [8]:
# regular
list(range(0,5))


Out[8]:
[0, 1, 2, 3, 4]

In [10]:
# numpy style (array range)
np.arange(0,5)


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

In [11]:
np.arange(1,11,2)


Out[11]:
array([1, 3, 5, 7, 9])

In [13]:
np.zeros(3)     # returns an array of three zeros


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

In [15]:
np.zeros((3,5))     # (rows, columns)


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

In [16]:
np.ones(5)


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

In [17]:
np.ones((3,2))


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

In [20]:
np.linspace(0,10,3)    #linearly spaced (spaced out evenly)


Out[20]:
array([  0.,   5.,  10.])

In [22]:
np.linspace(0,10,51)


Out[22]:
array([  0. ,   0.2,   0.4,   0.6,   0.8,   1. ,   1.2,   1.4,   1.6,
         1.8,   2. ,   2.2,   2.4,   2.6,   2.8,   3. ,   3.2,   3.4,
         3.6,   3.8,   4. ,   4.2,   4.4,   4.6,   4.8,   5. ,   5.2,
         5.4,   5.6,   5.8,   6. ,   6.2,   6.4,   6.6,   6.8,   7. ,
         7.2,   7.4,   7.6,   7.8,   8. ,   8.2,   8.4,   8.6,   8.8,
         9. ,   9.2,   9.4,   9.6,   9.8,  10. ])

In [25]:
np.eye(4)  # identity matrix in numpy


Out[25]:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

Numpy random library viz rand, randn and randint


In [30]:
# numpy random library'
# uniform distribution
np.random.rand(1)


Out[30]:
array([ 0.35158277])

In [31]:
np.random.rand(5,4)


Out[31]:
array([[ 0.88448113,  0.26555054,  0.98446944,  0.79993107],
       [ 0.84970839,  0.34934669,  0.14440586,  0.96875425],
       [ 0.97965177,  0.44525634,  0.60172707,  0.37763311],
       [ 0.19980857,  0.15335574,  0.6628985 ,  0.76584495],
       [ 0.83427905,  0.03143809,  0.99820794,  0.74113842]])

In [33]:
# normal distribution
np.random.randn(5)    # 5 numbers from normal distribution


Out[33]:
array([-0.0564426 ,  0.71612586, -0.28651379, -0.41846108,  0.89693003])

In [34]:
np.random.randn(5,4)


Out[34]:
array([[ 1.97140084, -0.51647091,  0.38947449,  1.61143302],
       [ 0.83226739, -0.57598199,  1.24518377,  0.32992451],
       [-0.41373008, -0.04412963,  0.38876499, -0.35845171],
       [ 1.01777484,  1.83326036,  0.08315832,  0.73114686],
       [-0.73782969, -0.15842273, -0.61232603,  0.11075342]])

In [35]:
np.random.randint(1,100)   # random integer


Out[35]:
79

In [36]:
np.random.randint(1,100,5)


Out[36]:
array([84, 36, 42,  8, 11])

In [37]:
np.random.randint(1,100,(5,4))


Out[37]:
array([[76, 86, 16, 51],
       [53, 57,  3, 72],
       [91, 40, 69, 92],
       [22, 48, 79, 69],
       [ 2, 90, 88, 94]])

reshape, shape, dtype


In [38]:
# reshaping arrays in numpy
arr = np.arange(25)
randarr = np.random.randint(0,50,10)

In [39]:
print(arr)
print(randarr)


[ 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]
[45 11  6 38 25 45 45 42  2 19]

In [41]:
arr.reshape(5,5)    # since 5 x 5 = 25


Out[41]:
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]])

In [42]:
arr.shape


Out[42]:
(25,)

In [43]:
arr.reshape(5,5).shape


Out[43]:
(5, 5)

In [44]:
arr.reshape(25,1)


Out[44]:
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]])

In [47]:
arr.reshape(25,1).shape   #difference between (25,) and (25,1)


Out[47]:
(25, 1)

In [48]:
arr.dtype    # datatype attribute


Out[48]:
dtype('int32')

max, argmax, min, argmin


In [50]:
randarr.max()   # max() method


Out[50]:
45

In [51]:
randarr.argmax()   # returns the index location of maximum value


Out[51]:
0

In [52]:
randarr.min()   # min() method


Out[52]:
2

In [53]:
randarr.argmin()    # returns the index location of minimum value


Out[53]:
8

Numpy Operations


In [54]:
import numpy as np

In [55]:
arr = np.arange(0,10)
arr


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

In [56]:
arr + arr    # elementwise addition


Out[56]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

In [57]:
arr * arr    # elementwise multiplication


Out[57]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

In [58]:
arr - arr    # elementwise subtraction


Out[58]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [60]:
arr / arr    # elementwise division, #this gives zero divide by zero warning


E:\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: RuntimeWarning: invalid value encountered in true_divide
  if __name__ == '__main__':
Out[60]:
array([ nan,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.])

In [62]:
1 / arr  # infinity warning


E:\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: RuntimeWarning: divide by zero encountered in true_divide
  if __name__ == '__main__':
Out[62]:
array([        inf,  1.        ,  0.5       ,  0.33333333,  0.25      ,
        0.2       ,  0.16666667,  0.14285714,  0.125     ,  0.11111111])

In [63]:
arr ** 3


Out[63]:
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729], dtype=int32)

In [64]:
arr + 100


Out[64]:
array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])

In [65]:
# taking square root 
np.sqrt(arr)


Out[65]:
array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ,
        2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ])

In [66]:
# taking exponential
np.exp(arr)


Out[66]:
array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,
         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,
         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,
         8.10308393e+03])

In [67]:
np.max(arr)


Out[67]:
9

In [68]:
arr.max()


Out[68]:
9

In [70]:
np.sin(arr)


Out[70]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])

In [72]:
np.log(arr)   # log of zero warning


E:\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: RuntimeWarning: divide by zero encountered in log
  if __name__ == '__main__':
Out[72]:
array([       -inf,  0.        ,  0.69314718,  1.09861229,  1.38629436,
        1.60943791,  1.79175947,  1.94591015,  2.07944154,  2.19722458])

Numpy Indexing and Selection


In [73]:
import numpy as np

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

In [75]:
arr


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

In [78]:
# grabing notations
arr[:5]


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

In [79]:
arr[6:]


Out[79]:
array([ 6,  7,  8,  9, 10])

In [80]:
arr[3:6]


Out[80]:
array([3, 4, 5])

In [82]:
# broadcasting
arr


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

In [85]:
arr[0:5] = 100   # awesome :D

In [86]:
arr


Out[86]:
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])

VERY IMPORTANT!!!


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


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

In [89]:
slice_of_arr = arr[:6]
slice_of_arr


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

In [91]:
slice_of_arr[:] = 99
slice_of_arr


Out[91]:
array([99, 99, 99, 99, 99, 99])

In [93]:
arr    # original arr changes with slice_of_arr


Out[93]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

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


Out[94]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

In [95]:
arr_copy[:] = 33
arr_copy


Out[95]:
array([33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33])

In [96]:
arr


Out[96]:
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

In [97]:
# Indexing
# mat[row, col]]
# mat[row][col]

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


Out[98]:
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])

In [99]:
mat[0]


Out[99]:
array([ 5, 10, 15])

In [100]:
mat[-1]


Out[100]:
array([35, 40, 45])

In [101]:
mat[1,1]


Out[101]:
25

In [102]:
mat[1][1]


Out[102]:
25

In [103]:
# 2-d array slicing
mat


Out[103]:
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])

In [105]:
# grab everything uptill row index 2 and grab from col index 1 to everything
mat[:2,1:]


Out[105]:
array([[10, 15],
       [25, 30]])

In [106]:
mat[1:,:2]


Out[106]:
array([[20, 25],
       [35, 40]])

In [109]:
# conditional selection
arr = np.arange(1,11)
arr


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

In [110]:
arr > 4


Out[110]:
array([False, False, False, False,  True,  True,  True,  True,  True,  True], dtype=bool)

In [111]:
bool_arr = arr > 4

In [112]:
arr[bool_arr]


Out[112]:
array([ 5,  6,  7,  8,  9, 10])

In [113]:
arr[arr > 4]


Out[113]:
array([ 5,  6,  7,  8,  9, 10])

In [115]:
arr[arr <= 9]


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

In [ ]: