Discovering the environment

The partical lab is based on the following tools :

  • python : a general purpose language
    • modern language / very clean / not limited
    • gentle learning curve - easy for beginners
  • python standard library
    • HUGE
  • jupyter notebook an interactive layer over the python language
    • a nice GUI of this stuff, also includes R and other environments
    • many added convenient utilities added
    • check the %magic commands
  • numpy a library making numeric vectors first class python objects
  • scipy a library holding most standards mathematical and statistical methods
    • other library available.
  • matplotlib a library for scientific plots

there are also libraries that we will not use, but can be very usefull for data-scientists:

  • pandas is a very comprehensie data-analysis environment for python - similar to R
  • SymPy is a symbolic environment similar to Mapple
  • scikit-learn a machine-earning envirnment for python

getting documentation

This is a very important part - you'll find yourself spending more time reading doc than writting code !

Do not hesitate to go through the different help systems - available from here, ( look at the Help menu of this page, you will recognize the list )

Notebook

it is organized in cells, each cell can either contain formatted text ( this cell! ), or a python program.

When executed, the result is displayed below the program cell

command

  • shell type
In [1]:
pwd
Out[1]:
u'/Users/mad/Documents/ mad/cours/FT/Fourier_Transform'

there is a slight difficult with python version 2 ane 3 co-exist. In practice, there is very little differences, unless you dig deep into the environment. At our level, the following command insures that both versions will behave in an equivalent manner.

In [1]:
from __future__ import print_function, division    # this insures python 2 / python 3 compatibility
  • python command
In [2]:
print( 'this course will last', 1 + 0.5, 'hours')
this course will last 1.5 hours
  • python programs
In [3]:
def fibonacci(n):
    "the simplest (and inefficient) definition of the fibonacci series"
    if n == 0 or n == 1:
        f = 1
    else:
        f = fibonacci(n-1) + fibonacci(n-2)
    return f

for i in range(10):
    print (i+1, ':', fibonacci(i+1))
1 : 1
2 : 2
3 : 3
4 : 5
5 : 8
6 : 13
7 : 21
8 : 34
9 : 55
10 : 89

the python language

python is a simple program, one of its strength lies in all the libraries available - in particular scientific ones (see above)

Some are directly a part of the standard language (web interface, cryptogrphy, data-base, etc.) Others are developped independently, with the standard scientific stack : numpy, scipy, sympy, matplotlib, pandas (look at the help menu) and others more specific to a particular domain.

In [4]:
# numpy provides a fast computation of large numerical arrays
import numpy as np     # (here we just give numpy the (standard) nick name np for easing the source code)

# matplotlib is the graphic library
# matplotlib.pylab is an easy to use utility, a bit reminiscent to matlab graphics
import matplotlib as mpl
import matplotlib.pylab as plt

# %matplotlib is a "magic" command to insert directly the graphics in the web page
%matplotlib inline

with these tools we can easily build and display complex functions

In [5]:
x = np.linspace(0,10,1000)    # 1000 points equi distant from 0.0 to 10.0
y = np.cos(x)                 # takes the cos() values of all points in x
print('length of vectors, x:',len(x), 'y:',len(y))
print('433th value:', x[432], y[432])    # !!! array indices are from 0 to 999 !!
length of vectors, x: 1000 y: 1000
433th value: 4.32432432432 -0.378397687089
In [6]:
# and plot it - like in Excel !
plt.plot(x,y)
plt.plot(x[432],y[432], 'ro')   # r is for red   o is for round points
Out[6]:
[<matplotlib.lines.Line2D at 0x109dfbf90>]