Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
413 lines (290 sloc) 8.9 KB

Jupyter

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.

Install

pip install jupyter

Run

$ jupyter notebook
[I 08:58:24.417 NotebookApp] Serving notebooks from local directory: /Users/catherine
[I 08:58:24.417 NotebookApp] 0 active kernels
[I 08:58:24.417 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 08:58:24.417 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

$ jupyter notebook filename.ipynb
$ jupyter notebook --port 9999

Using

  • Add code
  • Run code
  • Modify code and run
  • Autocomplete
  • Cell type (Markdown, LaTeX, Code)

Shortcut keys

Indent

  • Tab
  • Shift + Tab

Comment Code

  • Ctrl + /

Cells

Insert Below/Above Cells

Add, Delete Cells

Cut, Copy, Paste Cells

Move Up/Down Cells

Merge, Split Cells

Run

Run Cell

  • Shift-Enter

Run All (above/below)

Clear Output

Magic commands

  • %run
  • !pip freeze

Kernels

Functions

Checkpoints

Download

Trust Notebook

Close and Halt

Performance and profiling

  • %%timeit

Markdown

Unorganized lists

* first element
* second element
* third element
- first element
- second element
- third element

Organized lists

1. first element
1. second element
1. third element

Headers

# Header level 1
## Header level 2
### Header level 3
#### Header level 4
##### Header level 5
###### Header level 6

Formatting

*italic*
**bold**

Code inline

`class`

Code blocks

```python
name = 'Jose Jimenez'
print(f'My name... {name}')
```

Tables

| id | first_name | last_name |    agency |
|----|:-----------|:---------:|----------:|
| 1  | José       |  Jiménez  |      NASA |
| 2  | Иван       |  Иванович | Roscosmos |
| 3  | Mark       |   Watney  |      NASA |
| 4  | Alex       |   Vogel   |      NASA |

Embedding objects

LaTeX

  • %%latex
%%latex

$$c = \sqrt{a^2 + b^2}$$
%%latex

$$\int_{x=0}^{x=\infty} x^\pi dx$$
%%latex

\begin{equation}
H← ​​​60 ​+​ \frac{​​30(B-R)​​}{Vmax-Vmin}  ​​, if V​max​​ = G
\end{equation}
from IPython.display import display, Math, Latex

display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

Matplotlib charts

%matplotlib inline
import math
import random
from matplotlib import pyplot as plt

x1 = [x*0.01 for x in range(0,628)]
y1 = [math.sin(x*0.01)+random.gauss(0, 0.1) for x in range(0,628)]
plt.plot(x1, y1)

x2 = [x*0.5 for x in range(0,round(63/5))]
y2 = [math.cos(x*0.5) for x in range(0,round(63/5))]
plt.plot(x2, y2, 'o-')

plt.show()

HTML and Javascript

from IPython.display import Javascript, HTML

Javascript("alert('It is JavaScript!')")
HTML("We can <i>generate</i> <code>html</code> code <b>directly</b>!")

JavaScript

Image

YouTube

from IPython.display import YouTubeVideo
YouTubeVideo("wupToqz1e2g")

Workflow

import pandas as pd

url = 'https://raw.githubusercontent.com/scikit-learn/scikit-learn/master/sklearn/datasets/data/iris.csv'
columns = ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species']

data = pd.read_csv(url, skiprows=1, names=columns)
head = data.head(5)
#   Sepal length  Sepal width  Petal length  Petal width  Species
# 0           5.1          3.5           1.4          0.2        0
# 1           4.9          3.0           1.4          0.2        0
# 2           4.7          3.2           1.3          0.2        0
# 3           4.6          3.1           1.5          0.2        0
# 4           5.0          3.6           1.4          0.2        0
data.tail(3)
#      Sepal length  Sepal width  Petal length  Petal width  Species
# 147           6.5          3.0           5.2          2.0        2
# 148           6.2          3.4           5.4          2.3        2
# 149           5.9          3.0           5.1          1.8        2

# data.loc[data.Species == 0, 'Species'] = 'setosa'
# data.loc[data.Species == 1, 'Species'] = 'versicolor'
# data.loc[data.Species == 2, 'Species'] = 'virginica'
species = {
    0: 'setosa',
    1: 'versicolor',
    2: 'virginica'
}
# Change column Species values
data.Species.replace(to_replace=species, inplace=True)
# Shuffle columns and reset indexes
data.sample(frac=1).reset_index(drop=True, inplace=True)
#      Sepal length  Sepal width     ...      Petal width     Species
# 0             5.0          2.0     ...              1.0  versicolor
# 1             6.4          2.7     ...              1.9   virginica
# 2             5.6          3.0     ...              1.5  versicolor
# 3             5.7          2.6     ...              1.0  versicolor
# 4             6.4          3.1     ...              1.8   virginica
# 5             4.6          3.6     ...              0.2      setosa
# 6             5.9          3.0     ...              1.5  versicolor
data.describe()
#        Sepal length  Sepal width  Petal length  Petal width
# count    150.000000   150.000000    150.000000   150.000000
# mean       5.843333     3.057333      3.758000     1.199333
# std        0.828066     0.435866      1.765298     0.762238
# min        4.300000     2.000000      1.000000     0.100000
# 25%        5.100000     2.800000      1.600000     0.300000
# 50%        5.800000     3.000000      4.350000     1.300000
# 75%        6.400000     3.300000      5.100000     1.800000
# max        7.900000     4.400000      6.900000     2.500000

Execute terminal commands

  • !

  • !pwd

  • !ls

  • dirs = !ls
    
    for file in dirs:
        if file.find("1_") >= 0:
            print(file)
    

Output to different formats

File -> Download as:

  • Notebook (.ipynb)
  • Python (.py)
  • HTML (.html)
  • Reveal.js Slides (.html)
  • Markdown (.md)
  • reST (.rst)
  • LaTeX (.lex)
  • PDF via LaTeX (.pdf)

Generate HTML

jupyter nbconvert --to html --template basic mynotebook.ipynb

Slides

View -> Cell Toolbar -> Slideshow

# First run will generate config and may exit with error!
# In such case, rerun the line

jupyter nbconvert filename.ipynb --to slides --post serve

Github pages with Jupyter Slides

git submodule add https://github.com/hakimel/reveal.js.git reveal.js

jupyter nbconvert --to slides index.ipynb --reveal-prefix=reveal.js

jupyter nbconvert --to slides index.ipynb --reveal-prefix=reveal.js \
    --SlidesExporter.reveal_theme=serif \
    --SlidesExporter.reveal_scroll=True \
    --SlidesExporter.reveal_transition=none

Assignments

Podstawy korzystania

  1. Stwórz notebook jupyter o nazwie first.ipynb
  2. Dodaj tekst opisujący następne polecenia
  3. Dodaj trzy różne 'Code Cell'
  4. Uruchom Code Cell z wynikiem wszystkich powyżej
  5. Dodaj Code Cell, który pokaże czas wykonywania instrukcji
  6. Dodaj Code Cell, który wyświetli wykres funkcji sin() inplace

Slajdy

  1. Poprzedni skrypt przekonwertuj na slajdy i uruchom prezentację w przeglądarce