Best practices

Let's start with pep8 (https://www.python.org/dev/peps/pep-0008/)

Imports should be grouped in the following order:

  • standard library imports
  • related third party imports
  • local application/library specific imports

You should put a blank line between each group of imports. Put any relevant all specification after the imports.


In [1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format='retina' 

# Add this to python2 code to make life easier
from __future__ import absolute_import, division, print_function

from itertools import combinations
import string

from IPython.display import IFrame, HTML, YouTubeVideo
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.pyplot import GridSpec
import seaborn as sns
import mpld3
import numpy as np
import pandas as pd
import os, sys
import warnings

sns.set();
plt.rcParams['figure.figsize'] = (12, 8)
sns.set_style("darkgrid")
sns.set_context("poster", font_scale=1.3)

In [2]:
YouTubeVideo("ZbrRrXiWBKc", width=400, height=300)


Out[2]:

In [3]:
!conda install pivottablejs -y


Fetching package metadata .........
Solving package specifications: ..........

# All requested packages already installed.
# packages in environment at /Users/jonathan/miniconda3/envs/py3:
#
pivottablejs              0.1.0                    py35_0  

In [4]:
df = pd.read_csv("../data/mps.csv", encoding="ISO-8859-1")

In [5]:
df.head(10)


Out[5]:
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 [6]:
from pivottablejs import pivot_ui

Enhanced Pandas Dataframe Display


In [7]:
pivot_ui(df)
# Province, Party, Average, Age, Heatmap


Out[7]:

Keyboard shortcuts

For help, ESC + h


In [8]:
# in select mode, shift j/k (to select multiple cells at once)
# split cell with ctrl shift -

In [9]:
first = 1

In [10]:
second = 2

In [11]:
third = 3

You can also get syntax highlighting if you tell it the language that you're including:

mkdir toc
cd toc

wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.js

wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.css
cd ..

jupyter-nbextension install --user toc
jupyter-nbextension enable toc/toc
SELECT *
FROM tablename

In [12]:
%%bash
pwd 
for i in *.ipynb
do
    wc $i
done
echo 
echo "break"
echo
du -h *ipynb


/Users/jonathan/github/AstroHackWeek2016/notebook-tutorial/notebooks
     108     343    2905 00-Overview.ipynb
     767    1589   97891 01-Tips-and-tricks.ipynb
     133     250    2410 02-Visualization-and-code-organization.ipynb
     921    2234  499442 03-Pandas-and-Plotting.ipynb
     492    1144   13128 04-SQL-Example.ipynb
     169     381   23836 05-interactive-splines.ipynb
     393     841  204811 06-R-stuff.ipynb
    1069    2174   20704 07-Some_basics.ipynb
    1028    2025   17918 08-More_basics.ipynb
     422     943    8408 09-Extras.ipynb
     569    1672   21017 Data_Cleaning.ipynb

break

4.0K	00-Overview.ipynb
 96K	01-Tips-and-tricks.ipynb
4.0K	02-Visualization-and-code-organization.ipynb
488K	03-Pandas-and-Plotting.ipynb
 16K	04-SQL-Example.ipynb
 24K	05-interactive-splines.ipynb
204K	06-R-stuff.ipynb
 24K	07-Some_basics.ipynb
 20K	08-More_basics.ipynb
 12K	09-Extras.ipynb
 24K	Data_Cleaning.ipynb

In [13]:
def silly_absolute_value_function(xval):
    """Takes a value and returns the value."""
    xval_sq = xval ** 2.0
    xval_abs = np.sqrt(xval_sq)
    return xval_abs

In [14]:
silly_absolute_value_function?

In [15]:
silly_absolute_value_function??

In [ ]:
# shift-tab
silly_absolute_value_function()

In [ ]:
# shift-tab-tab
silly_absolute_value_function()

In [ ]:
# shift-tab-tab-tab
silly_absolute_value_function()

In [5]:
import numpy as np

In [14]:
np.sin??

Stop here for now


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [2]:
import numpy as np

In [3]:
# !conda install -c r rpy2 -y

In [4]:
import rpy2

In [ ]:
%load_ext rpy2.ipython

In [24]:
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])

In [17]:
%%R?


Object `%%R` not found.

In [28]:
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)


Call:
lm(formula = Y ~ X)

Residuals:
   1    2    3    4    5 
-0.2  0.9 -1.0  0.1  0.2 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   3.2000     0.6164   5.191   0.0139 *
X             0.9000     0.2517   3.576   0.0374 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.7958 on 3 degrees of freedom
Multiple R-squared:   0.81,	Adjusted R-squared:  0.7467 
F-statistic: 12.79 on 1 and 3 DF,  p-value: 0.03739


In [29]:
XYcoef


Out[29]:
array([ 3.2,  0.9])

In [ ]: