EPITA 2021 MLRF practice_01-01_jupyter v2021-05-17_160644 by Joseph CHAZALON
This first session is based on a Jupyter notebook, like the future ones. We this this is a great format because:
It may be a good idea to spent 10 minutes learning a few tricks about Jupyter. We'll focus on:
You can safely skip this session if you already have experience using Jupyter.
Jupyter has two modes:
If you are using Chrome (which exhibited better compatibility in our tests), you can toggle the command palette by pressing Ctrl+Shift+P at any time.
You can change how cells are interpreted. In particular, 2 cell types are of interest:
You can also insert, merge, copy, paste, delete, move cells.
Running a cell is so easy!
Jupyter is a tool to display notebooks and run code in kernels.
It uses iPython to interactively run Python code.
iPython provides much more features than the default Python interpreter, among which:
!
, !!
or var = !shell_command
constructs which allows both to run shell commands and capture their output in a very easy way (and you can pass Python variables to the shell using $
constructs;%
magics) or for a cell (%%
magics);?
and ??
constructs with optional wildcards.# TODO Call this cell!
%quickref
# TODO fix this cell
notebooks = !echo nothing
notebooks[:10]
Let's complete this preambule by measuring how much time is necessary to perform a simple computation.
Here is a simple function which return a positive integer for any positive integer > 0.
def collatz_total_stopping_time(n):
if not n > 0:
raise ValueError("n must be > 0")
nn = n
i = 0
while nn > 1:
if nn % 2 == 0:
nn = nn / 2
else:
nn = nn * 3 + 1
i += 1
return i
collatz_total_stopping_time(63728127)
Finally, let's try to find which numbers in $[1, 100000]$ produces the greatest output.
max_i = 0
max_n = 1
for n in range(1, 100000+1):
i = collatz_total_stopping_time(n)
if i > max_i:
max_i = i
max_n = n
max_n, max_i
And, to complete this short list of tips and tricks, my favorite: the online help.
You can add ?
at the beginning or the end of a line to display the help about the related symbol.
# TODO run this cell
zip?
You can also use wildcards to display all matching symbols in the current namespace!
This can be particularly useful with OpenCV to locate some functions.
You can use this trick like this:
import cv2
cv2.*onnected*?
Now you're ready to move on to the next stage: Sharpening your NumPy-fu skills.