Basic of Python.

The library we are going to use are the following:

  • numpy

In [6]:
for i in range(10):
   y = i*40 +i**2 + 2
   print(y)
    
i = i+2


2
43
86
131
178
227
278
331
386
443

In [7]:
range(10)


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

In [8]:
for i in ["a","b","tt"]:
    print i


a
b
tt

In [20]:
x = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],[2, 1, 2, 3, 4, 5, 6, 7, 8, 9],[0,5, 2, 3, 4, 5, 6, 7, 8, 9]]
print(x)


[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 5, 2, 3, 4, 5, 6, 7, 8, 9]]

In [23]:
np.mean(x)


Out[23]:
4.7000000000000002

In [24]:
x= np.linspace(-1,1,100)

In [25]:
x


Out[25]:
array([-1.        , -0.97979798, -0.95959596, -0.93939394, -0.91919192,
       -0.8989899 , -0.87878788, -0.85858586, -0.83838384, -0.81818182,
       -0.7979798 , -0.77777778, -0.75757576, -0.73737374, -0.71717172,
       -0.6969697 , -0.67676768, -0.65656566, -0.63636364, -0.61616162,
       -0.5959596 , -0.57575758, -0.55555556, -0.53535354, -0.51515152,
       -0.49494949, -0.47474747, -0.45454545, -0.43434343, -0.41414141,
       -0.39393939, -0.37373737, -0.35353535, -0.33333333, -0.31313131,
       -0.29292929, -0.27272727, -0.25252525, -0.23232323, -0.21212121,
       -0.19191919, -0.17171717, -0.15151515, -0.13131313, -0.11111111,
       -0.09090909, -0.07070707, -0.05050505, -0.03030303, -0.01010101,
        0.01010101,  0.03030303,  0.05050505,  0.07070707,  0.09090909,
        0.11111111,  0.13131313,  0.15151515,  0.17171717,  0.19191919,
        0.21212121,  0.23232323,  0.25252525,  0.27272727,  0.29292929,
        0.31313131,  0.33333333,  0.35353535,  0.37373737,  0.39393939,
        0.41414141,  0.43434343,  0.45454545,  0.47474747,  0.49494949,
        0.51515152,  0.53535354,  0.55555556,  0.57575758,  0.5959596 ,
        0.61616162,  0.63636364,  0.65656566,  0.67676768,  0.6969697 ,
        0.71717172,  0.73737374,  0.75757576,  0.77777778,  0.7979798 ,
        0.81818182,  0.83838384,  0.85858586,  0.87878788,  0.8989899 ,
        0.91919192,  0.93939394,  0.95959596,  0.97979798,  1.        ])

In [29]:
x[[34,67]]


Out[29]:
array([-0.31313131,  0.35353535])

In [30]:
x[45:90]


Out[30]:
array([-0.09090909, -0.07070707, -0.05050505, -0.03030303, -0.01010101,
        0.01010101,  0.03030303,  0.05050505,  0.07070707,  0.09090909,
        0.11111111,  0.13131313,  0.15151515,  0.17171717,  0.19191919,
        0.21212121,  0.23232323,  0.25252525,  0.27272727,  0.29292929,
        0.31313131,  0.33333333,  0.35353535,  0.37373737,  0.39393939,
        0.41414141,  0.43434343,  0.45454545,  0.47474747,  0.49494949,
        0.51515152,  0.53535354,  0.55555556,  0.57575758,  0.5959596 ,
        0.61616162,  0.63636364,  0.65656566,  0.67676768,  0.6969697 ,
        0.71717172,  0.73737374,  0.75757576,  0.77777778,  0.7979798 ])

In [31]:
x[::3]


Out[31]:
array([-1.        , -0.93939394, -0.87878788, -0.81818182, -0.75757576,
       -0.6969697 , -0.63636364, -0.57575758, -0.51515152, -0.45454545,
       -0.39393939, -0.33333333, -0.27272727, -0.21212121, -0.15151515,
       -0.09090909, -0.03030303,  0.03030303,  0.09090909,  0.15151515,
        0.21212121,  0.27272727,  0.33333333,  0.39393939,  0.45454545,
        0.51515152,  0.57575758,  0.63636364,  0.6969697 ,  0.75757576,
        0.81818182,  0.87878788,  0.93939394,  1.        ])

In [32]:
x[10::3]


Out[32]:
array([-0.7979798 , -0.73737374, -0.67676768, -0.61616162, -0.55555556,
       -0.49494949, -0.43434343, -0.37373737, -0.31313131, -0.25252525,
       -0.19191919, -0.13131313, -0.07070707, -0.01010101,  0.05050505,
        0.11111111,  0.17171717,  0.23232323,  0.29292929,  0.35353535,
        0.41414141,  0.47474747,  0.53535354,  0.5959596 ,  0.65656566,
        0.71717172,  0.77777778,  0.83838384,  0.8989899 ,  0.95959596])

In [33]:
x[::-1]


Out[33]:
array([ 1.        ,  0.97979798,  0.95959596,  0.93939394,  0.91919192,
        0.8989899 ,  0.87878788,  0.85858586,  0.83838384,  0.81818182,
        0.7979798 ,  0.77777778,  0.75757576,  0.73737374,  0.71717172,
        0.6969697 ,  0.67676768,  0.65656566,  0.63636364,  0.61616162,
        0.5959596 ,  0.57575758,  0.55555556,  0.53535354,  0.51515152,
        0.49494949,  0.47474747,  0.45454545,  0.43434343,  0.41414141,
        0.39393939,  0.37373737,  0.35353535,  0.33333333,  0.31313131,
        0.29292929,  0.27272727,  0.25252525,  0.23232323,  0.21212121,
        0.19191919,  0.17171717,  0.15151515,  0.13131313,  0.11111111,
        0.09090909,  0.07070707,  0.05050505,  0.03030303,  0.01010101,
       -0.01010101, -0.03030303, -0.05050505, -0.07070707, -0.09090909,
       -0.11111111, -0.13131313, -0.15151515, -0.17171717, -0.19191919,
       -0.21212121, -0.23232323, -0.25252525, -0.27272727, -0.29292929,
       -0.31313131, -0.33333333, -0.35353535, -0.37373737, -0.39393939,
       -0.41414141, -0.43434343, -0.45454545, -0.47474747, -0.49494949,
       -0.51515152, -0.53535354, -0.55555556, -0.57575758, -0.5959596 ,
       -0.61616162, -0.63636364, -0.65656566, -0.67676768, -0.6969697 ,
       -0.71717172, -0.73737374, -0.75757576, -0.77777778, -0.7979798 ,
       -0.81818182, -0.83838384, -0.85858586, -0.87878788, -0.8989899 ,
       -0.91919192, -0.93939394, -0.95959596, -0.97979798, -1.        ])

In [34]:
x= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [35]:
x[::-1]


Out[35]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [37]:
y = np.linspace(0,10,100)
x= np.linspace(-1,1,100)

In [38]:
x


Out[38]:
array([-1.        , -0.97979798, -0.95959596, -0.93939394, -0.91919192,
       -0.8989899 , -0.87878788, -0.85858586, -0.83838384, -0.81818182,
       -0.7979798 , -0.77777778, -0.75757576, -0.73737374, -0.71717172,
       -0.6969697 , -0.67676768, -0.65656566, -0.63636364, -0.61616162,
       -0.5959596 , -0.57575758, -0.55555556, -0.53535354, -0.51515152,
       -0.49494949, -0.47474747, -0.45454545, -0.43434343, -0.41414141,
       -0.39393939, -0.37373737, -0.35353535, -0.33333333, -0.31313131,
       -0.29292929, -0.27272727, -0.25252525, -0.23232323, -0.21212121,
       -0.19191919, -0.17171717, -0.15151515, -0.13131313, -0.11111111,
       -0.09090909, -0.07070707, -0.05050505, -0.03030303, -0.01010101,
        0.01010101,  0.03030303,  0.05050505,  0.07070707,  0.09090909,
        0.11111111,  0.13131313,  0.15151515,  0.17171717,  0.19191919,
        0.21212121,  0.23232323,  0.25252525,  0.27272727,  0.29292929,
        0.31313131,  0.33333333,  0.35353535,  0.37373737,  0.39393939,
        0.41414141,  0.43434343,  0.45454545,  0.47474747,  0.49494949,
        0.51515152,  0.53535354,  0.55555556,  0.57575758,  0.5959596 ,
        0.61616162,  0.63636364,  0.65656566,  0.67676768,  0.6969697 ,
        0.71717172,  0.73737374,  0.75757576,  0.77777778,  0.7979798 ,
        0.81818182,  0.83838384,  0.85858586,  0.87878788,  0.8989899 ,
        0.91919192,  0.93939394,  0.95959596,  0.97979798,  1.        ])

careful with "=":

  • xx= x means they are the same object
  • xx = x .... whatever or x.copy() they are two different objects

In [47]:
xx = x.copy()

In [48]:
xx+=2

In [49]:
xx


Out[49]:
array([ 3.        ,  3.02020202,  3.04040404,  3.06060606,  3.08080808,
        3.1010101 ,  3.12121212,  3.14141414,  3.16161616,  3.18181818,
        3.2020202 ,  3.22222222,  3.24242424,  3.26262626,  3.28282828,
        3.3030303 ,  3.32323232,  3.34343434,  3.36363636,  3.38383838,
        3.4040404 ,  3.42424242,  3.44444444,  3.46464646,  3.48484848,
        3.50505051,  3.52525253,  3.54545455,  3.56565657,  3.58585859,
        3.60606061,  3.62626263,  3.64646465,  3.66666667,  3.68686869,
        3.70707071,  3.72727273,  3.74747475,  3.76767677,  3.78787879,
        3.80808081,  3.82828283,  3.84848485,  3.86868687,  3.88888889,
        3.90909091,  3.92929293,  3.94949495,  3.96969697,  3.98989899,
        4.01010101,  4.03030303,  4.05050505,  4.07070707,  4.09090909,
        4.11111111,  4.13131313,  4.15151515,  4.17171717,  4.19191919,
        4.21212121,  4.23232323,  4.25252525,  4.27272727,  4.29292929,
        4.31313131,  4.33333333,  4.35353535,  4.37373737,  4.39393939,
        4.41414141,  4.43434343,  4.45454545,  4.47474747,  4.49494949,
        4.51515152,  4.53535354,  4.55555556,  4.57575758,  4.5959596 ,
        4.61616162,  4.63636364,  4.65656566,  4.67676768,  4.6969697 ,
        4.71717172,  4.73737374,  4.75757576,  4.77777778,  4.7979798 ,
        4.81818182,  4.83838384,  4.85858586,  4.87878788,  4.8989899 ,
        4.91919192,  4.93939394,  4.95959596,  4.97979798,  5.        ])

In [50]:
x


Out[50]:
array([ 1.        ,  1.02020202,  1.04040404,  1.06060606,  1.08080808,
        1.1010101 ,  1.12121212,  1.14141414,  1.16161616,  1.18181818,
        1.2020202 ,  1.22222222,  1.24242424,  1.26262626,  1.28282828,
        1.3030303 ,  1.32323232,  1.34343434,  1.36363636,  1.38383838,
        1.4040404 ,  1.42424242,  1.44444444,  1.46464646,  1.48484848,
        1.50505051,  1.52525253,  1.54545455,  1.56565657,  1.58585859,
        1.60606061,  1.62626263,  1.64646465,  1.66666667,  1.68686869,
        1.70707071,  1.72727273,  1.74747475,  1.76767677,  1.78787879,
        1.80808081,  1.82828283,  1.84848485,  1.86868687,  1.88888889,
        1.90909091,  1.92929293,  1.94949495,  1.96969697,  1.98989899,
        2.01010101,  2.03030303,  2.05050505,  2.07070707,  2.09090909,
        2.11111111,  2.13131313,  2.15151515,  2.17171717,  2.19191919,
        2.21212121,  2.23232323,  2.25252525,  2.27272727,  2.29292929,
        2.31313131,  2.33333333,  2.35353535,  2.37373737,  2.39393939,
        2.41414141,  2.43434343,  2.45454545,  2.47474747,  2.49494949,
        2.51515152,  2.53535354,  2.55555556,  2.57575758,  2.5959596 ,
        2.61616162,  2.63636364,  2.65656566,  2.67676768,  2.6969697 ,
        2.71717172,  2.73737374,  2.75757576,  2.77777778,  2.7979798 ,
        2.81818182,  2.83838384,  2.85858586,  2.87878788,  2.8989899 ,
        2.91919192,  2.93939394,  2.95959596,  2.97979798,  3.        ])

Masking

This only works with numpy array.

numpy array vs. list


In [52]:
xlist = [3,4,5,6,7,8,9]
xarray = np.asarray([3,4,5,6,7,8,9]) # np.asarray(xlist)

In [53]:
xlist*2


Out[53]:
[3, 4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7, 8, 9]

In [54]:
xarray*2


Out[54]:
array([ 6,  8, 10, 12, 14, 16, 18])

In [56]:
strangelist = ["toto",3,{},[]]

In [60]:
np.asarray(strangelist)*2


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-ad3befd67480> in <module>()
----> 1 np.asarray(strangelist)*2

TypeError: unsupported operand type(s) for *: 'dict' and 'int'

how to apply masking?

Use Numpy ARRAY


In [64]:
x


Out[64]:
array([ 1.        ,  1.02020202,  1.04040404,  1.06060606,  1.08080808,
        1.1010101 ,  1.12121212,  1.14141414,  1.16161616,  1.18181818,
        1.2020202 ,  1.22222222,  1.24242424,  1.26262626,  1.28282828,
        1.3030303 ,  1.32323232,  1.34343434,  1.36363636,  1.38383838,
        1.4040404 ,  1.42424242,  1.44444444,  1.46464646,  1.48484848,
        1.50505051,  1.52525253,  1.54545455,  1.56565657,  1.58585859,
        1.60606061,  1.62626263,  1.64646465,  1.66666667,  1.68686869,
        1.70707071,  1.72727273,  1.74747475,  1.76767677,  1.78787879,
        1.80808081,  1.82828283,  1.84848485,  1.86868687,  1.88888889,
        1.90909091,  1.92929293,  1.94949495,  1.96969697,  1.98989899,
        2.01010101,  2.03030303,  2.05050505,  2.07070707,  2.09090909,
        2.11111111,  2.13131313,  2.15151515,  2.17171717,  2.19191919,
        2.21212121,  2.23232323,  2.25252525,  2.27272727,  2.29292929,
        2.31313131,  2.33333333,  2.35353535,  2.37373737,  2.39393939,
        2.41414141,  2.43434343,  2.45454545,  2.47474747,  2.49494949,
        2.51515152,  2.53535354,  2.55555556,  2.57575758,  2.5959596 ,
        2.61616162,  2.63636364,  2.65656566,  2.67676768,  2.6969697 ,
        2.71717172,  2.73737374,  2.75757576,  2.77777778,  2.7979798 ,
        2.81818182,  2.83838384,  2.85858586,  2.87878788,  2.8989899 ,
        2.91919192,  2.93939394,  2.95959596,  2.97979798,  3.        ])

In [65]:
mask = x>2

In [66]:
mask


Out[66]:
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,  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,  True], dtype=bool)

In [68]:
x[mask] # x[x>2]


Out[68]:
array([ 2.01010101,  2.03030303,  2.05050505,  2.07070707,  2.09090909,
        2.11111111,  2.13131313,  2.15151515,  2.17171717,  2.19191919,
        2.21212121,  2.23232323,  2.25252525,  2.27272727,  2.29292929,
        2.31313131,  2.33333333,  2.35353535,  2.37373737,  2.39393939,
        2.41414141,  2.43434343,  2.45454545,  2.47474747,  2.49494949,
        2.51515152,  2.53535354,  2.55555556,  2.57575758,  2.5959596 ,
        2.61616162,  2.63636364,  2.65656566,  2.67676768,  2.6969697 ,
        2.71717172,  2.73737374,  2.75757576,  2.77777778,  2.7979798 ,
        2.81818182,  2.83838384,  2.85858586,  2.87878788,  2.8989899 ,
        2.91919192,  2.93939394,  2.95959596,  2.97979798,  3.        ])

In [76]:
x[ (x>2) & (x<2.5)  ] # x[ (x>2) * (x>1.5)  ] # both have to be true


Out[76]:
array([ 2.01010101,  2.03030303,  2.05050505,  2.07070707,  2.09090909,
        2.11111111,  2.13131313,  2.15151515,  2.17171717,  2.19191919,
        2.21212121,  2.23232323,  2.25252525,  2.27272727,  2.29292929,
        2.31313131,  2.33333333,  2.35353535,  2.37373737,  2.39393939,
        2.41414141,  2.43434343,  2.45454545,  2.47474747,  2.49494949])

In [75]:
x[ (x>2) | (x>1.5)  ] # x[ (x>2) + (x>1.5)  ] # any have to be true


Out[75]:
array([ 1.50505051,  1.52525253,  1.54545455,  1.56565657,  1.58585859,
        1.60606061,  1.62626263,  1.64646465,  1.66666667,  1.68686869,
        1.70707071,  1.72727273,  1.74747475,  1.76767677,  1.78787879,
        1.80808081,  1.82828283,  1.84848485,  1.86868687,  1.88888889,
        1.90909091,  1.92929293,  1.94949495,  1.96969697,  1.98989899,
        2.01010101,  2.03030303,  2.05050505,  2.07070707,  2.09090909,
        2.11111111,  2.13131313,  2.15151515,  2.17171717,  2.19191919,
        2.21212121,  2.23232323,  2.25252525,  2.27272727,  2.29292929,
        2.31313131,  2.33333333,  2.35353535,  2.37373737,  2.39393939,
        2.41414141,  2.43434343,  2.45454545,  2.47474747,  2.49494949,
        2.51515152,  2.53535354,  2.55555556,  2.57575758,  2.5959596 ,
        2.61616162,  2.63636364,  2.65656566,  2.67676768,  2.6969697 ,
        2.71717172,  2.73737374,  2.75757576,  2.77777778,  2.7979798 ,
        2.81818182,  2.83838384,  2.85858586,  2.87878788,  2.8989899 ,
        2.91919192,  2.93939394,  2.95959596,  2.97979798,  3.        ])

The case of the NaN Value


In [78]:
iamnan = np.NaN

In [79]:
iamnan


Out[79]:
nan

In [80]:
iamnan==iamnan


Out[80]:
False

In [82]:
np.inf==np.inf


Out[82]:
True

In [83]:
xwithnan = np.asarray([3,4,5,6,7,2,3,np.NaN,75,75])

In [84]:
xwithnan


Out[84]:
array([  3.,   4.,   5.,   6.,   7.,   2.,   3.,  nan,  75.,  75.])

In [85]:
xwithnan*2


Out[85]:
array([   6.,    8.,   10.,   12.,   14.,    4.,    6.,   nan,  150.,  150.])

In [86]:
4+np.NaN


Out[86]:
nan

In [87]:
4/np.NaN


Out[87]:
nan

In [88]:
4**np.NaN


Out[88]:
nan

In [89]:
np.mean(xwithnan)


Out[89]:
nan

In [90]:
np.nanmean(xwithnan)


Out[90]:
20.0

In [91]:
np.mean(xwithnan[xwithnan==xwithnan])


Out[91]:
20.0

In [96]:
~(xwithnan==xwithnan)


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

In [93]:
xwithnan!=xwithnan


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

In [97]:
np.isnan(xwithnan)


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

In [98]:
xwithnan = [3,4,5,6,7,2,3,np.NaN,75,75]

In [100]:
xwithnan[xwithnan==xwithnan]


Out[100]:
4

In [105]:
0 == False


Out[105]:
True

In [ ]:
1 == True

Your first plot

For ploting we are going to use matplotlib. let's plot 2 random variable a vs. b


In [117]:
a = np.random.rand(30)
b = np.random.rand(30)

In [118]:
# plot within the notebook
%matplotlib inline
import matplotlib.pyplot as mpl

In [119]:
pl = mpl.hist(a)



In [126]:
mpl.scatter(a,b,s=150, facecolors="None", edgecolors="b",lw=3)


Out[126]:
<matplotlib.collections.PathCollection at 0x1067e86d0>

In [ ]: