"# math.sqrt (pour square root) calcule la racine carrée\n",
"import math\n",
...
...
%% Cell type:markdown id: tags:
# Jupyter Notebooks Guide
%% Cell type:markdown id: tags:
The course material of this Mooc is proposed in **Jupyter notebooks** which are documents that contain live code, equations, visualizations and narrative text. To manipulate these notebooks, the open-source web application Jupyter has been integrated into this Mooc. It does not require any installation on your side. All data and calculations will be hosted on our servers.
This document is an example of a Jupyter Notebook. It presents you the Jupyter interface and how tu use the notebooks.
Please note that **code parts can be evaluated and modified**. So you can easily try out variants around the original notebook. Don't hésitate because you will be able to reset to the original version of the notebook.
%% Cell type:markdown id: tags:
## Table of content
- How to use Jupyter Notebooks
- How to restart the kernel
- Shutdown running notebooks
- References about Jupyter notebook
- Installing Jupyter on your computer
%% Cell type:markdown id: tags:
## How to use Jupyter Notebooks
### Executing Python code
%% Cell type:markdown id: tags:
At the top of the notebook, you have a menu bar, containing:
* a menu bar with the entries `File`, `Edit`, `Insert`, `Cell`, `Kernel`;
* and a button bar that are shortcuts to some frequently used menus. If you leave your mouse over a button, a small text will appear, indicating what function the button is for.
A notebook consists of a series of cells, either textual or containing code. The code cells are easily recognisable, they are preceded by `In [ ]:`. The cell following the one you are reading is a code cell.
To start, select this code cell below with your mouse (the line preceded by `In [ ]:`), and press in the menu bar the "Run button" (the one surrounded by a red circle) :
As you can see, the cell is "executed" (more commonly called evaluated), and we move on to the next cell.
When a code cell has been evaluated, Jupyter adds under the cell `In` a cell `Out` which gives the result of the Python code, above `600`.
Jupyter also adds a number between the square brackets to display, for example above, `In [1]:` and `Out [1]:`. This number allows you to retrieve the order in which the cells were evaluated..
Alternatively, you can simply type on the keyboard ***Shift+Enter***, to get the same effect. In general, it is important to learn and use the keyboard shortcuts, this will save you a lot of time later.
%% Cell type:markdown id: tags:
If you want to *execute* the entire notebook step by step, you have to:
* select the first cell of the notebook,
* and press ***Shift+Enter*** until the notebook is finished.
%% Cell type:markdown id: tags:
It is also possible to evaluate the whole notebook at once by using the menu *Cell -> Run All*.
%% Cell type:markdown id: tags:
You can of course modify these code cells for testing purposes, so you can use the model below to calculate the square root of 2, or try the function on a negative number and see how the error is reported.
%% Cell type:code id: tags:
``` python
# math.sqrt (pour square root) calcule la racine carrée
importmath
math.sqrt(2)
```
%% Output
1.4142135623730951
%% Cell type:markdown id: tags:
### Be careful to evaluate the cells in the right order
%% Cell type:markdown id: tags:
It is important that the code cells are evaluated in the correct order. If you do not respect the order in which the code cells are presented, the result may be unexpected.
In fact, evaluating a programme in the form of a notebook is like cutting it up into small fragments, and if these fragments are executed in a disorderly manner, the result is naturally a different programme.
%% Cell type:markdown id: tags:
This can be seen in this example:
%% Cell type:code id: tags:
``` python
message="Be careful about the order in which you evaluate the notebooks."
```
%% Cell type:code id: tags:
``` python
print(message)
```
%% Cell type:markdown id: tags:
If further on the notebook we have the following cell:
%% Cell type:code id: tags:
``` python
# this has the effect of deleting the variable 'message'.
delmessage
```
%% Cell type:markdown id: tags:
which makes the symbol `message` indefinite, then of course we can no longer evaluate the cell which makes `print` since the variable `message` is no longer known to the interpreter.
%% Cell type:markdown id: tags:
### You are working on a copy
%% Cell type:markdown id: tags:
One of the main advantage of notebooks is that you can modify the code we have written, and see for yourself how the modified code behaves.
For this reason, each student has his/her **own copy** of each notebook, you can of course make any changes you wish to your notebooks without affecting other students.
%% Cell type:markdown id: tags:
### Back to original the course version
%% Cell type:markdown id: tags:
You can always return to the "course" version through the *File → Reset to original* menu.
Be careful, with this function you restore **the whole notebook** and therefore **you loose your modifications on it**.
Note the *File → Reset all to original* menu allows you to **reset to original version all the notebooks of the Mooc**. If you use it, it takes a few minutes.
%% Cell type:markdown id: tags:
### Download in other formats
%% Cell type:markdown id: tags:
You can download a notebook in Python, pdf, html... format to your computer through the menu
*File → Download as → Python*
For the Python download, text cells are preserved in the result as Python comments.
%% Cell type:markdown id: tags:
### Adding cells
%% Cell type:markdown id: tags:
You can add a cell anywhere in the document with the **+** button on the button bar (or with the menu *Insert → Insert Cell Above* or *Insert → Insert Cell Below* ).
Also, when you reach the end of the document, a new cell is created each time you evaluate the last cell; this way you have a draft for your own tests.
When you create a new cell, you have to select its type between Markdown, Python.
**Markdown is lightweight markup languages**. Github proposes a short and efficient introduction: <ahref="https://guides.github.com/features/mastering-markdown/">Mastering Markdown</a>.
%% Cell type:markdown id: tags:
## How to restart the kernel
%% Cell type:markdown id: tags:
If you made too many changes, or loose track of what you have evaluated, it may be useful to restart your interpreter:
* The *Kernel → Restart* menu allows you to do this, a bit like IDLE which starts from a blank interpreter when you use the F5 function.
* The *Kernel → Restart and Clear Output* menu restarts the kernel and clears all the outputs.
* The *Kernel → Interrupt* menu can be used if your fragment takes too long to run (e.g. you have written a loop whose logic is broken and does not finish).
* The *Kernel → shutdown* menu can be used to close a running notebook if you need to free memory for exemple.
%% Cell type:markdown id: tags:
## Shutdown running notebooks
The *File → Running notebooks* menu displays the running notebooks.
Then, Click on each *Shutdown* button to stop the process. It may be very usefull if you have opened too many notebooks.
There are many documentations and tutorials about Jupyter notebooks, here are a few of them:
* This [web page on dataquest blog](https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/) contains a number of tips on how to use Jupyter (including illustrations of the many IPython magic commands) that can improve your efficiency.
* This [Youtube playlist](https://www.youtube.com/playlist?list=PLRJx8WOUx5XcDMOxSQegCJUjTJePTlF9Z) contains many tutorials on the use of notebooks.
%% Cell type:markdown id: tags:
## Installing Jupyter on your computer
If you prefer to work in standalone mode, you may install Jupyter on your computer. **[Miniconda](https://conda.io/miniconda.html)**, a lightweight edition of Anaconda, is a software distribution that includes **Python, R, Jupyter, and many popular libraries for scientific computing and data science**.
You can find all information about **scikit-learn installation on the [scikit-learn site](https://scikit-learn.org/stable/install.html#install-official-release)**.
At last,you will find all the **notebooks and datasets files in the Mooc file github repository**: [https://github.com/INRIA/scikit-learn-mooc].