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 [2]:
# Best practice for loading libraries?
# Couldn't find what to do with 'magic' imports at the top

%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format='retina' 

from __future__ import division

from itertools import combinations
import string

from IPython.display import IFrame, HTML, YouTubeVideo
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy as sp
import seaborn as sns; sns.set();

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


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
/Users/jonathan/anaconda/envs/anaconda_r/lib/python2.7/site-packages/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

In [3]:
YouTubeVideo("ZbrRrXiWBKc", width=800, height=600)


Out[3]:

In [4]:
!pip install pivottablejs


Collecting pivottablejs
  Using cached pivottablejs-0.1.0-py2.py3-none-any.whl
Installing collected packages: pivottablejs
Successfully installed pivottablejs-0.1.0

In [5]:
df = pd.read_csv("../data/mps.csv")

In [6]:
df.head()


Out[6]:
Name Party Province Age Gender
0 Liu, Laurin NDP Quebec 22 Female
1 Mourani, Maria Bloc Quebecois Quebec 43 Female
2 Sellah, Djaouida NDP Quebec NaN Female
3 St-Denis, Lise NDP Quebec 72 Female
4 Fry, Hedy Liberal British Columbia 71 Female

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


Out[7]:

Keyboard shortcuts


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

In [9]:
first = 1

second = 2

third = 3

Floating Table of Contents

Creates a new button on the toolbar that pops up a table of contents that you can navigate by.

In your documentation if you indent by 4 spaces, you get monospaced code-style code so you can embed in a Markdown cell:

$ 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

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

In [10]:
import rpy2

In [11]:
%load_ext rpy2.ipython

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

In [13]:
%%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

Tech_Vault additions

Miniconda + conda environments

Probably the best way to go -- there should be an updated document in tech_vault that describes the way to setup py2, and py3 environments.

SVDS Template

  • color/seaborn template
  • standards of organization (imports all at the top)

R&D Project: Projects that would be great to support


In [ ]: