What is a Jupyter Notebook?

From the Jupyter website (http://jupyter.org):

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.

Some helpful resources:

  • Anaconda is a very easy way to get started with Jupyter notebooks for Windows, Mac and Linux environments
  • A gallery of interesting notebooks includes tutorials, and notebooks published in academic journals
  • MyBinder is a new way to share your notebooks interactively
  • nbextensions offers a range of useful notebook extensions
  • Jupyterlab is the next generation Jupyter notebook, with some very nice new features. It is still under development, but is worth getting familiar with

A small Demo:


In [36]:
# code goes into these boxes ("cells")
# cells are executed consecutively, with the output printed immediately beneath the cell

In [37]:
print("Welcome to SMARTFest 2018!")


Welcome to SMARTFest 2018!

Text can be entered into cells by designating it for markdown, allowing simple formatting.

It is also possible to enter Latex formulae into markdown cells:

$$ p_{obs} = \frac{(k+1)\cdot p_{linear}}{k \cdot p_{linear}+1} $$

Graphical output can also be incorporated:


In [38]:
%matplotlib inline

In [39]:
import matplotlib.pyplot as plt

In [40]:
# import a practice dataset

from sklearn import datasets 

diabetes = datasets.load_diabetes()
bmi = diabetes.data[2]
bp = diabetes.data[3]

In [41]:
plt.scatter(bmi,bp);



In [7]:
# add a line of best fit:

import numpy as np

z = np.polyfit(x=bmi, y=bp, deg=1)
p = np.poly1d(z)
trendline = p(sorted(bmi))

In [8]:
plt.scatter(bmi,bp)
plt.plot(sorted(bmi),trendline);


Sharing your notebook

To allow others to interact with your notebook, create a github repository of the notebook, data, and dependencies. Then share using mybinder

Widgets for easy interaction!


In [9]:
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

In [10]:
def f(x):
    return 3*x^2 - 2*x + 8

In [11]:
interact(f, x=10);



In [16]:
def g(mu,sigma):
    s = np.random.normal(mu, sigma, 1000)
    count, bins, ignored = plt.hist(s, 30, normed=True)
    plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
             linewidth=2, color='r')
    plt.xlim((-5,5))
    plt.ylim((0,3))
    plt.show()

In [18]:
interact(g, 
         mu=widgets.FloatSlider(min=-4,max=4,step=0.5,value=0), 
         sigma=widgets.FloatSlider(min=0.1,max=3,step=0.1,value=0.1));



In [33]:
def random_plot(sigma=0.85):
    x = np.random.randint(0,50,100)
    s = sigma * np.random.randn(100)
    y = x + (10 * s)
    plt.scatter(x,y)

In [34]:
random_plot(0.1)



In [35]:
interact(random_plot, 
         sigma=widgets.FloatSlider(min=0.1,max=1,step=0.1,value=0.5));