Python Installation

For this course you need to install Python and some extra packages. We will be using Python 3 and assuming you will be using it too.

The easiest way to get up and running using Python is to download the Anaconda Python Distribution. For the three major platforms Linux, Windows and Mac we have installation instructions available:

Anaconda Python

We strongly prefer Anaconda for Python software development. The main reason is that it doesn’t mess up with your system installed Python. A lot of the tools your are using everyday on your computer are really Python programs, all these programs use the system installed version of the Python interpreter.

When developing software, especially in case your are using third party state of the art packages and libraries, you will not be the first to really mess up. Versions conflicts of libraries are occurring frequently and sometimes to the extend that your Python system software is not working anymore.

Then there is Anaconda to prevent this. It installs a second complete Python eco system on your computer entirely in user space. Just a word of warning: the standard when installing Anaconda is that Anaconda Python will take over your system Python. I strongly advise NOT to do that. It is not too much work to switch to Anaconda Python when doing real programming.

Details are given for the different operating systems.

Linux

  • Download Anaconda from https://www.continuum.io/downloads

  • Open a terminal window (command line window) and type:

    bash Anaconda3-4.4.0-Linux-x86_64.sh
    

    or whatever the name of the downloaded file is. Follow the instructions, BUT DO NOT ACCEPT THE DEFAULT TO CHANGE THE PATHS.

The consequence of not having accepted the default to change your path settings is that you need to this on a as needed basis. Assuming that /home/<user>/anaconda3 is the path to your Anaconda distribution you can start using Anaconda by opening a command window and typing:

which python
~/anaconda3/bin/activate root
which python

If all went well you will see that the default python program has changed. Not only python but also pip, jupyter and a lot of other python related programs are now from the Anaconda distribution. Evidently the which python commands are not needed really...

Windows

Follow the instructions given in Anaconda documentation <https://docs.continuum.io/anaconda/install/windows>. Pay attention in step 8 and do NOT add Anaconda to the PATH environment variable.

MacOS

Follow the instructions given in Anaconda documentation <https://docs.continuum.io/anaconda/install/mac-os>.

IPython Notebooks

Python is an interpreted language and that makes it perfect for a REPL way of designing programs. REPL stands for Read-Eval-Print-Loop. You type in a command (a short piece of code), that code is run (interpreted) and the results are printed (or displayed or plotted or rendered as sound or...).

IPython is a shell (we hope you are familiar with the concept of using a computer using the shell) which makes it nice to work with Python in an interactive REPL setting.

IPython takes the REPL concept one step further (of course greatly influenced by programs like MathCad en Mathematica) and provides the concept of IPython notebooks.

Notebooks are files containing both the input to a REPL shell as well as the output together with the text even with math formula’s that go with it.

All our LabExercises will be provided as Ipython Notebooks. You can download these files, add your own code, run the code to produce output and then hand in the Notebook for us to grade.

Notebooks are visualized and used in your browser (i know that Chrome and Firefox are working). A simple example of what a notebook looks like on your screen is given below:

simpleNotebook

Just a Simple Example Notebook

In [1]:
%pylab notebook
Populating the interactive namespace from numpy and matplotlib

This a text cell where you can type text that is not being interpreted as python code. The text is written in a format called Markdown. Please look into the help files to see what can be done in Markdown.

You can even do math formulas: $$ y = \sin(x) $$ Let's plot this function using Python, Numpy and Matplotlib.

In [17]:
x = linspace(0, 2*pi, 20)
y = sin(x)
print(vstack((x,y)).T)
[[  0.00000000e+00   0.00000000e+00]
 [  3.30693964e-01   3.24699469e-01]
 [  6.61387927e-01   6.14212713e-01]
 [  9.92081891e-01   8.37166478e-01]
 [  1.32277585e+00   9.69400266e-01]
 [  1.65346982e+00   9.96584493e-01]
 [  1.98416378e+00   9.15773327e-01]
 [  2.31485774e+00   7.35723911e-01]
 [  2.64555171e+00   4.75947393e-01]
 [  2.97624567e+00   1.64594590e-01]
 [  3.30693964e+00  -1.64594590e-01]
 [  3.63763360e+00  -4.75947393e-01]
 [  3.96832756e+00  -7.35723911e-01]
 [  4.29902153e+00  -9.15773327e-01]
 [  4.62971549e+00  -9.96584493e-01]
 [  4.96040945e+00  -9.69400266e-01]
 [  5.29110342e+00  -8.37166478e-01]
 [  5.62179738e+00  -6.14212713e-01]
 [  5.95249134e+00  -3.24699469e-01]
 [  6.28318531e+00  -2.44929360e-16]]
In [19]:
plot(x, y, 'b+-')
Out[19]:
[<matplotlib.lines.Line2D at 0x7f6255724d30>]
In [ ]:
 

Assuming the installation of Python went ok you can start browsing, editing and running notebooks in the following way: open a command line terminal and type:

cd MyMLNotebookDirectory
jupyter notebook

You should see messages in the command window telling you that the Jupyter server is started. It will also open a browser (or use an open browser) and display a page showing the files in your MyMLNotebooksDirectory (probably none for now...). You can also make a new notebook and start experimenting with Python.

You can also download the simpleNotebook.ipynb file, store it in your directory and open this one. Please make yourself comfortable with using IPython Notebooks. Read the documentation.