In [37]:
import time
import platform
print('Last updated: %s' %time.strftime('%d/%m/%Y'))
print('Created using Python', platform.python_version())
Last updated: 27/06/2014
Created using Python 3.4.1

I would be happy to hear your comments and suggestions.
Please feel free to drop me a note via twitter, email, or google+.

Awesome things that you can do in IPython Notebooks (in progress)

Writing local files

In [13]:
def func_inside_script(x, y):
    return x + y
print('Hello World')

Running Python scripts

We can run Python scripts in IPython via the %run magic command. For example, the Python script that we created in the Writing local files section.

In [14]:
Hello World
In [16]:
func_inside_script(1, 2)


In [4]:
%timeit [x**2 for x in range(100)] 
10000 loops, best of 3: 38.8 µs per loop
In [3]:
%timeit -r 5 -n 100 [x**2 for x in range(100)] 
100 loops, best of 5: 39 µs per loop

Using system shell commands

By prepending a "!" we can conveniently execute most of the system shell commands, below are just a few examples.

In [12]:
my_dir = 'new_dir'
!mkdir $my_dir
!touch $my_dir'/some.txt'
!ls -l './new_dir'
!ls -l $my_dir | wc -l
total 0
-rw-r--r--  1 sebastian  staff  0 Jun 27 10:11 some.txt


In [1]:
def some_func():
    var = 'hello world'
    for i in range(5):
    i / 0
    return 'finished'
In [ ]:
> <ipython-input-1-3d5f00f75cf4>(5)some_func()
      4         print(i)
----> 5     i / 0
      6     return 'finished'

Inline Plotting with matplotlib

In [33]:
%matplotlib inline
In [36]:
import numpy as np
from matplotlib import pyplot as plt
import math

def pdf(x, mu=0, sigma=1):
    """Calculates the normal distribution's probability density 
        function (PDF).  
    term1 = 1.0 / ( math.sqrt(2*np.pi) * sigma )
    term2 = np.exp( -0.5 * ( (x-mu)/sigma )**2 )
    return term1 * term2

x = np.arange(0, 100, 0.05)

pdf1 = pdf(x, mu=5, sigma=2.5**0.5)
pdf2 = pdf(x, mu=10, sigma=6**0.5)

plt.plot(x, pdf1)
plt.plot(x, pdf2)
plt.title('Probability Density Functions')
plt.xlabel('random variable x')
plt.legend(['pdf1 ~ N($\mu=5$, $\sigma=2.5$)', 'pdf2 ~ N($\mu=10$, $\sigma=6$)'], loc='upper right')

C-extensions via the Cython magic

Cython (see Cython's C-extensions for Python) is basically a hybrid between C and Python and can be pictured as compiled Python code with type declarations. Since we are working in an IPython notebook here, we can make use of the very convenient IPython magic: It will take care of the conversion to C code, the compilation, and eventually the loading of the function. Also, we are adding C type declarations; those type declarations are not necessary for using Cython, however, it will improve the performance of our code significantly.

In [29]:
%load_ext cythonmagic
In [30]:
import numpy as np
cimport numpy as np
cimport cython
cpdef cython_lstsqr(x_ary, y_ary):
    """ Computes the least-squares solution to a linear matrix equation. """
    cdef double x_avg, y_avg, var_x, cov_xy,\
         slope, y_interc, temp
    cdef double[:] x = x_ary # memoryview
    cdef double[:] y = y_ary
    cdef unsigned long N, i
    N = x.shape[0]
    x_avg = 0
    y_avg = 0
    for i in range(N):
        x_avg += x[i]
        y_avg += y[i]
    x_avg = x_avg/N
    y_avg = y_avg/N
    var_x = 0
    cov_xy = 0
    for i in range(N):
        temp = (x[i] - x_avg)
        var_x += temp**2
        cov_xy += temp*(y[i] - y_avg)
    slope = cov_xy / var_x
    y_interc = y_avg - slope*x_avg
    return (slope, y_interc)
building '_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df' extension
C compiler: /usr/bin/clang -fno-strict-aliasing -Werror=declaration-after-statement -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/sebastian/miniconda3/envs/py34/include -arch x86_64

compile options: '-I/Users/sebastian/miniconda3/envs/py34/lib/python3.4/site-packages/numpy/core/include -I/Users/sebastian/miniconda3/envs/py34/include/python3.4m -c'
clang: /Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.c
/usr/bin/clang -bundle -undefined dynamic_lookup -L/Users/sebastian/miniconda3/envs/py34/lib -arch x86_64 /Users/sebastian/.ipython/cython/Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.o -L/Users/sebastian/miniconda3/envs/py34/lib -o /Users/sebastian/.ipython/cython/
In [31]:
import numpy as np

x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)])
y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)])
In [32]:
cython_lstsqr(x_ary, y_ary)
(1.1399825800539194, 2.0824398156005444)

Running Fortran Code

There is also a convenient IPython magic command for compiling Fortran code. The Fortran magic uses NumPy's F2PY module for compiling and running the Fortran code. For more information, please see the 'Fortran magic's documentation'.

In [17]:
Installed To use it, type:
  %load_ext fortranmagic
In [18]:
%load_ext fortranmagic
In [22]:
SUBROUTINE fortran_lstsqr(ary_x, ary_y, slope, y_interc)
    ! Computes the least-squares solution to a linear matrix equation. """
    REAL(8), INTENT(in), DIMENSION(:) :: ary_x, ary_y
    REAL(8), INTENT(out) :: slope, y_interc
    REAL(8) :: x_avg, y_avg, var_x, cov_xy, temp
    INTEGER(8) :: N, i
    N = SIZE(ary_x)

    x_avg = SUM(ary_x) / N
    y_avg = SUM(ary_y) / N
    var_x = 0
    cov_xy = 0
    DO i = 1, N
        temp = ary_x(i) - x_avg
        var_x = var_x + temp**2
        cov_xy = cov_xy + (temp*(ary_y(i) - y_avg))
    END DO
    slope = cov_xy / var_x
    y_interc = y_avg - slope*x_avg

END SUBROUTINE fortran_lstsqr
	Building module "_fortran_magic_a044885f2b0c0feac78a230b6b714e2b"...
		Constructing wrapper function "fortran_lstsqr"...
		  slope,y_interc = fortran_lstsqr(ary_x,ary_y)
	Wrote C/API module "_fortran_magic_a044885f2b0c0feac78a230b6b714e2b" to file "/var/folders/bq/_946cdn92t7bqzz5frpfpw7r0000gp/T/tmp3y_jxtl_/src.macosx-10.5-x86_64-3.4/_fortran_magic_a044885f2b0c0feac78a230b6b714e2bmodule.c"
	Fortran 77 wrappers are saved to "/var/folders/bq/_946cdn92t7bqzz5frpfpw7r0000gp/T/tmp3y_jxtl_/src.macosx-10.5-x86_64-3.4/_fortran_magic_a044885f2b0c0feac78a230b6b714e2b-f2pywrappers.f"
In [23]:
import numpy as np

x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)])
y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)])
In [25]:
fortran_lstsqr(x_ary, y_ary)
(1.1313508052697814, 3.681685640167956)

Running code from other interpreters: Ruby, Perl, and Bash

To use any interpreter that is installed on your system:

In [44]:
%%script perl
print 'Hello, World!';
Hello, World!

Or use the magic command for the respective interpreter directly:

In [45]:
print 'Hello, World!';
Hello, World!
In [46]:
puts "Hello, World!"
Hello, World!
In [47]:
echo "Hello World!"
Hello World!
In [55]:
%%script R --no-save
cat("Goodbye, World!\n")
R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin10.8.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> cat("Goodbye, World!\n")
Goodbye, World!
In [7]:
def hello_world():
    """This is a hello world example function."""
    print('Hello, World!')
In [9]:
%pdoc hello_world
In [10]:
%pdef hello_world
In [15]:
%psource math.mean()
Object `math.mean()` not found.
In [16]:
from math import sqrt
ImportError                               Traceback (most recent call last)
<ipython-input-16-fdd1a06c836a> in <module>()
----> 1 from math import mean

ImportError: cannot import name 'mean'
In [ ]: