P4DA IPython Development Environment

Act without doing; work without effort. Think of the small as large and the few as many. Confront the difficult while it is still easy; accomplish the great task by a series of small acts.

Magic Commands

A magical command is any command prefixed by the percent symbol %.

using the %timeit magic function:

import numpy as np  
a = np.random.randn(100, 100)  
%timeit np.dot(a, a)
#Output:
#The slowest run took 1727.17 times longer than the fastest. 
#This could mean that an intermediate result is being cached.
#10000 loops, best of 3: 27.7 ┬Ás per loop

Many of the magic commands have additional command line options, which can all be viewed using ?:

%reset?

%reset

Explore all of the special commands available by typing %quickref or %magic.

Frequently used magic commands

Matplotlib Integration and Pylab Mode

The typical way to launch IPython with matplotlib integration is by adding pylab flag

%pylab

pylab

IPython will launch with the default GUI backend integration enabled so that matplotlib plot windows can be created with no issues. Secondly, most of Numpy and matplotlib will be imported into the top level interactive namespace to produce and interactive computing environment.

Basic Profiling: %prun and %run p

The main Python profiling tool is the cProfile module, which is not specific to IPython at all.

A common way to use cProfile is on the command line, running an entire program and outputting the aggregated time per function. Suppose we had a simple script which does some linear algebra in a loop:

import numpy as np  
from numpy.linalg import eigvals

def run_experiment(niter = 100):  
    k = 100
    results = []
    for _ in xrange(niter):
        mat = np.random.randn(k, k)
        max_eigenvalue = np.abs(eigvals(mat)).max
        results.append(max_eigenvalue)
    return results

some_results = run_experiment()  
print 'Largest one we saw: %s' % np.max(some_results)  

And you can run this script through cProfile by running the following in the command line:

python -m cProfile cprof_example.py  

IPython has a convenient interface to this capability using the %prun command and the -p option to %run.

%prun -l 7 -s cumulative run_experiment()