Markdown Cell

Sub heading this is big text

Smaller subheadings

This is just a paragraph.


In [1]:
# code cell
name = "Jonathan"

Tips and Tricks

A markdown cell lets you do many useful things.


In [2]:
import numpy as np

# don't do:
# from numpy import *

In [3]:
max("a")


Out[3]:
'a'

In [4]:
np.max("a")


----------------------------------------------------------------------
TypeError                            Traceback (most recent call last)
<ipython-input-4-4005723e4b17> in <module>
----> 1 np.max("a")

<__array_function__ internals> in amax(*args, **kwargs)

~/miniconda3/envs/dspy3/lib/python3.6/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial, where)
   2619     """
   2620     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
-> 2621                           keepdims=keepdims, initial=initial, where=where)
   2622 
   2623 

~/miniconda3/envs/dspy3/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     88                 return reduction(axis=axis, out=out, **passkwargs)
     89 
---> 90     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     91 
     92 

TypeError: cannot perform reduce with flexible type

Imports


In [5]:
%matplotlib inline
%config InlineBackend.figure_format='retina'

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from pivottablejs import pivot_ui
import sys
import warnings

warnings.filterwarnings("ignore")

Keyboard shortcuts

Practice doing these a few times and try to force yourself to use them whenever you can.

Help (h)

For help in the Notebook, ESC + h (doesn't work in Lab)

Select multiple cells

j, k, and arrow keys while holding shift selects multiple cells.


In [6]:
first = 1

In [7]:
second = 2

In [8]:
third = 3

Split a cell with -

Well, ctrl + shift + -, but remember it by horizontal.


In [ ]:

Add a new cell (a)bove

Add a new cell (b)elow


In [9]:
print("oops")


oops

(d)(d)elete

unD(z)o

(c)opy cells

(v)aste cells

Pivot Tables w/ pandas

A library and example from: http://nicolas.kruchten.com/content/2015/09/jupyter_pivottablejs/


In [10]:
# df = pd.read_csv("../data/mps.csv", encoding="ISO-8859-1")
# canadian_politics = pd.read_csv("../data/montreal_2014.csv",)
canadian_politics = pd.read_csv("../data/mps2.csv")

In [11]:
# recommend using .head()
canadian_politics.head(10)


Out[11]:
Name Party Province Age Gender
0 Liu, Laurin NDP Quebec 22.0 Female
1 Mourani, Maria Bloc Quebecois Quebec 43.0 Female
2 Sellah, Djaouida NDP Quebec NaN Female
3 St-Denis, Lise NDP Quebec 72.0 Female
4 Fry, Hedy Liberal British Columbia 71.0 Female
5 Turmel, Nycole NDP Quebec 70.0 Female
6 Sgro, Judy Liberal Ontario 68.0 Female
7 Raynault, Francine NDP Quebec 67.0 Female
8 Davidson, Patricia Conservative Ontario 66.0 Female
9 Smith, Joy Conservative Manitoba 65.0 Female

In [12]:
sns.distplot(canadian_politics["Age"].dropna());



In [13]:
sns.set_context("poster", font_scale=1.3)

In [14]:
fig, ax = plt.subplots(figsize=(12, 8))
sns.distplot(canadian_politics["Age"].dropna())
fig.tight_layout()



In [ ]:
#gather

Enhanced Pandas Dataframe Display


In [ ]:
# Province, Party, Average, Age, Heatmap

In [17]:
pivot_ui(canadian_politics)


Out[17]:

In [16]:
canadian_politics['Age-bin'] = pd.cut(canadian_politics['Age'], [x for x in range(10, 100, 5)])

In [19]:
pd.read_clipboard(sep='\t')


Out[19]:
Province Female-Bloc Quebecois Female-Conservative Female-Green Female-Liberal Female-NDP Male-Bloc Quebecois Male-Conservative Male-Liberal Male-NDP
0 Alberta NaN 4.0 NaN NaN 1.0 NaN 23.0 NaN NaN
1 British Columbia NaN 5.0 1.0 2.0 3.0 NaN 16.0 NaN 9.0
2 Manitoba NaN 4.0 NaN NaN 1.0 NaN 7.0 1.0 1.0
3 New Brunswick NaN 1.0 NaN NaN NaN NaN 7.0 1.0 1.0
4 Newfoundland and Labrador NaN NaN NaN 1.0 NaN NaN 1.0 3.0 2.0
5 Nova Scotia NaN NaN NaN NaN 1.0 NaN 4.0 4.0 2.0
6 Ontario NaN 10.0 NaN 3.0 6.0 NaN 63.0 8.0 16.0
7 Prince Edward Island NaN 1.0 NaN NaN NaN NaN NaN 3.0 NaN
8 Quebec 1.0 NaN NaN NaN 27.0 3.0 5.0 7.0 32.0
9 Saskatchewan NaN 2.0 NaN NaN NaN NaN 11.0 1.0 NaN
10 Territories NaN 1.0 NaN NaN NaN NaN 1.0 NaN 1.0

Tab -- Your Friend


In [1]:
import numpy as np

In [2]:
from numpy.random import choice, chisquare

In [3]:
np.random.chisquare()


----------------------------------------------------------------------
TypeError                            Traceback (most recent call last)
<ipython-input-3-016675c2fa1d> in <module>
----> 1 np.random.chisquare()

mtrand.pyx in numpy.random.mtrand.RandomState.chisquare()

TypeError: chisquare() takes at least 1 positional argument (0 given)

In [4]:
# pure tab right ↓ less useful
np.random.choice()


----------------------------------------------------------------------
TypeError                            Traceback (most recent call last)
<ipython-input-4-c3640c1907bc> in <module>
      1 # pure tab right ↓ less useful
----> 2 np.random.choice()

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

TypeError: choice() takes at least 1 positional argument (0 given)

In [ ]:


In [ ]:


In [ ]:

shift-tab


In [ ]:
# shift-tab right ↓ more useful
np.linspace(start=50, stop=100, endpoint=False)

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:

shift-tab-tab


In [ ]:
np.

In [ ]:

shift-tab-tab-tab


In [ ]:
np.linspace(50, 150, num=100, endpoint=False)

shift-tab-tab-tab-tab


In [ ]:
plt.plot(np.linspace(start, stop))

In [ ]:


In [ ]:


In [ ]:


In [ ]:

?


In [5]:
np.linspace?

In [6]:
?np.linspace

??

(Lab can scroll if you click)


In [8]:
np.linspace??

In [9]:
!subl ~/miniconda3/envs/dspy3/lib/python3.6/site-packages/numpy/core/function_base.py

Scroll around without selecting cells

ALT + Up/Down

In [10]:
import textwrap
def example_function():
    """Docstring for example function"""
    
    print(textwrap.dedent("""
    This is a multi-lined string
    that I want to write inside of a function.
    Notice what happens when I print this.
        And when something is indented more."""))


example_function()


This is a multi-lined string
that I want to write inside of a function.
Notice what happens when I print this.
    And when something is indented more.

Inspect everything and Find and Replace


In [15]:
# But first find and replace
def silly_function(xval):
    """Takes a value and returns the value."""
    xval_sq = xval ** 2.0
    1 + 15
    xval_abs = np.sqrt(xval_sq)
    return xval_abs

In [16]:
silly_function(-2,)


Out[16]:
2.0

In [17]:
silly_function?

In [18]:
silly_function??

In [19]:
!ls ../data/


coal_prod_2008.csv           collincountyforeclosures.csv
coal_prod_2009.csv           example.csv
coal_prod_2010.csv           montreal_2014.csv
coal_prod_2011.csv           mps.csv
coal_prod_2012.csv           mps2.csv
coal_prod_cleaned.csv        nbextensions.png
coal_prod_cleaned.csv.zip    raw_players.csv.gz
coalpublic2012.xls

In [ ]:
coal_years = !ls ../data/coal_prod_20*.csv

In [ ]:
coal_years

Line numbers (lowercase "L")

Type lowercase "L" to have line numbers; shift-L for line numbers notebook-wide.

Move blocks of code around

Indent/dedent

Cmd + [
Cmd + ]

Comment

Cmd + /

In [21]:
ex_dictionary = {}

# Indent/dedent/comment
for index in range(5):
    ex_dictionary["integer_one"] = 1
    ex_dictionary["integer_two"] = 2
    ex_dictionary["integer_three"] = 3
    ex_dictionary["integer_four"] = 4

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:

Multicursor magic

Hold down option, click and drag (for big cursor).

cmd + click == wherever you click.

Shift command P -- command palette Classic Notebook Shift command C -- command palette in JupyterLab

Hide left bar (CMD + B)

Full screen CMD + Shift + D (but CTRL + CMD F for browser fullscreen)

Move cells around mouse


In [ ]:
example["one_better_neat"] = 1
example["two_better_neat"] = 2
example["three_better_neat"] = 3
example["four_better_neat"] = 4

Monospace

You can also get monospaced fonts by indenting 4 spaces:

mkdir toc
cd toc

Syntax Highlighting

Wrap with triple-backticks and language:

mkdir toc
cd toc
wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
SELECT first_name,
       last_name,
       year_of_birth
FROM presidents
WHERE year_of_birth > 1800;

Headings and LaTeX

With text and $\LaTeX$ support.

$$\begin{align} B'&=-\nabla \times E,\\ E'&=\nabla \times B - 4\pi j \end{align}$$

Magics

  • % $\equiv$ inline magic
  • %% $\equiv$ cell magic

In [22]:
%%latex

If you want to get crazier$\ldots$

\begin{equation}
\oint_S {E_n dA = \frac{1}{{\varepsilon _0 }}} Q_\textrm{inside}
\end{equation}


If you want to get crazier$\ldots$ \begin{equation} \oint_S {E_n dA = \frac{1}{{\varepsilon _0 }}} Q_\textrm{inside} \end{equation}

In [23]:
%%python2
print "hi"


hi

In [ ]:
%%bash
wget http://www.ast.cam.ac.uk/~rfc/vpfit11.1.tar.gz
mkdir -p vpfit11
cd vpfit11
tar -xvzf ../vpfit11.1.tar.gz

Scripting


In [24]:
normal_argument = 12.4
second_argument = 98.4

arg_with_spaces = "the secret to life"

In [25]:
%%bash -s {normal_argument} {second_argument}
echo "This script knows the value of the argument: $1"
echo "It also has no trouble with the second argument: $2"


This script knows the value of the argument: 12.4
It also has no trouble with the second argument: 98.4

In [26]:
%%bash -s "$arg_with_spaces"
echo "This bash script knows $1."


This bash script knows the secret to life.

In [ ]:
ls

In [ ]:
!ls

In [ ]:
ls vpfit10/

In [ ]:
tailthing = "*.ipynb"

In [ ]:
tailthing

In [ ]:
!ls {tailthing}

In [ ]:
output = !ls

In [ ]:
output

Need to set or change environment variables


In [ ]:
%env

In [ ]:
%env

Danger zone

_, _N and _iN

In [ ]:
!pwd

In [27]:
a = 3
a


Out[27]:
3

In [28]:
2345 * 97543


Out[28]:
228738335

In [29]:
_


Out[29]:
228738335

In [30]:
_27


Out[30]:
3

In [31]:
print(_i27)


a = 3
a

In [ ]:


In [34]:
saved = _27

In [35]:
saved


Out[35]:
3

In [ ]:
_

In [ ]:
_i55

In [36]:
%history -f alex.ipynb


File 'alex.ipynb' exists. Overwrite? y
Overwriting file.

Check out gather (microsoft)


In [ ]: