In [1]:
from IPython.display import display, Image, HTML
from talktools import website, nbviewer
IPython is an open source, interactive computing environment for Python and other languages.
In [2]:
website('http://ipython.org')
Out[2]:
In [3]:
import ipythonproject
In [4]:
ipythonproject.core_devs()
Notice that the output of the above Python code is an HTML table with embedded images. IPython generalizes the notion of output to include rich formats: HTML, PNG, JPEG, PDF, JavaScript, LaTeX, etc. This means that any Python object can declare rich representations that will be rendered and saved in the notebook.
The IPython Notebook is a web-based interactive computing environment that spans the full range of data related activities:
How does IPython target these different activities?
The central focus of IPython is the writing and running of code. We try to make this as pleasant as possible:
Let's download some stock data into a Pandas DataFrame
and then visualize the time series using Vincent/Vega/d3.
In [5]:
import vincent
import pandas as pd
vincent.initialize_notebook()
In [6]:
import pandas.io.data as web
all_data = {}
for ticker in ['AAPL', 'GOOG', 'IBM', 'YHOO', 'MSFT']:
all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2010', '1/1/2013')
price = pd.DataFrame({tic: data['Adj Close'] for tic, data in all_data.items()})
In the Notebook DataFrame
objects are represented as formatted HTML tables:
In [7]:
price[0:10]
Out[7]:
In [8]:
line = vincent.Line(price[['GOOG', 'AAPL', 'IBM', 'YHOO', 'MSFT']], width=600, height=300)
line.axis_titles(x='Date', y='Price')
line.legend(title='Ticker')
display(line)
Data science is a multi-language activity. R. Python. Julia. Scala. Etc. The IPython architecture is language agnostic.
Let's fit a linear model in R and visualize the results:
In [9]:
import numpy as np
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
%load_ext rmagic
The %%R
syntax tells IPython to run the rest of the cell as R code:
In [10]:
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
This %%language
syntax is an IPython specific extension to the Python language. This "magic command syntax" allows Python code to call out to a wide range of other languages (Ruby, Bash, Julia, Fortran, Perl, Octave, Matlab, etc.)
In the IPython architecture, the kernel is a separate process that runs the user's code and returns the output back to the frontend (Notebook, Terminal, etc.). Kernels talk to frontends using a well documented message protocol (JSON over ZeroMQ and WebSockets). The default kernel that ships with IPython knows how to run Python code. However, there are now kernels in other languages:
By later this year, all users of the IPython Notebook will have the option to choose what type of kernel to use for each Notebook.
Here is a notebook that runs code in the native Julia kernel:
In [11]:
website("http://nbviewer.ipython.org/url/jdj.mit.edu/~stevenj/IJulia%20Preview.ipynb")
Out[11]:
Notebook documents are just JSON files stored on your filesystem. These files store everything related to a computation:
Notebook documents can be shared:
Notebook documents can be viewed by anyone on the web through http://nbviewer.ipython.org
In [12]:
website("http://nbviewer.ipython.org")
Out[12]:
This allows people to compose and share reproducible stories that involve code and data.
Earlier this year, Randall Munroe (xkcd) published a comic about regular expression golf. Peter Norvig from Google wanted to explore some of the algorithms related to this comic and shared his explorations as a notebook on nbviewer:
In [13]:
website("http://nbviewer.ipython.org/url/norvig.com/ipython/xkcd1313.ipynb")
Out[13]:
In [ ]: