UPDATE (2015): This is an old repo, go here for the new edition
Examples from the IPython mini-book
This repository contains all code examples from the book Learning IPython for Interactive Computing and Data Visualization, Packt Publishing, by Cyrille Rossant.
Book overview
This book is a beginner-level introduction to IPython for interactive Python programming, high-performance numerical computing, and data visualization. It assumes nothing more than familiarity with Python. It targets developers, students, teachers, hobbyists who know Python a bit, and who want to learn IPython for the extended console, the Notebook, and for more advanced scientific applications.
The book first introduces IPython for interactive Python and shell programming. It shows how IPython can considerably improve the productivity of a developer who creates, debugs, benchmarks and profiles Python code.
Then, the reader learns the very basics of vector computing, and discovers how to load and analyze numerical and tabular data with NumPy and Pandas. The book shows the interactive visualization capabilities of the platform with Matplotlib, SciPy and PIL. It also contains a few image processing examples.
Some techniques to accelerate Python code are also demonstrated, using either interactive parallel computing features from IPython (using MPI or not), or Cython to compile a portion of the code in C for really interesting speedups.
Finally, the book shows how IPython can be customized for advanced uses, notably with the creation of new extensions and magic commands.
Code examples
Most files are IPython notebooks (.ipynb
extension with JSON data). There are also some Python external scripts (.py
extension).
The following modules are used in at least one example:
- Python 2.7
- IPython 0.13+
- NumPy
- Pandas
- SciPy
- Matplotlib
- Matplotlib basemap
- NetworkX
- PyQt or PySide
- Cython
- PIL
You can view the notebooks in the IPython notebook viewer (see links below).
Chapter 2: Interactive Work with IPython
-
Example 1. We download and extract a social graph dataset (ego graphs of anonymous Facebook users, obtained on the SNAP project from Stanford University). This example illustrates how to interact with the filesystem from IPython.
-
Example 2. Here we explore with IPython the dataset downloaded in the previous example.
-
Example 3. We use the NetworkX package to process social graphs. We show how to explore simultaneously the data and a new Python module as well in an IPython interactive session.
Chapter 3: Numerical Computing with IPython
-
Example 1. We introduce here the very basics of vector computing and NumPy multidimensional arrays. We show with a simple example what performance gains we can expect by using NumPy instead of pure Python loops.
-
Example 2. We generate multiplication tables with different methods, showing the performance benefits of using vectorized computations and broadcasting.
-
Example 3. We download and analyze a new dataset containing the geographical coordinates of all cities in the world, as well as the population for some of them. This free dataset has been created by MaxMind.
Chapter 4: Interactive Plotting and Graphical Interfaces
-
Example 1. This is a short introduction to the most basic plotting features of Matplotlib.
-
Example 2. We use the world cities dataset from the previous example to create a human density world map with SciPy and Matplotlib.
-
Example 3. We show how to plot histograms of social data.
-
Example 4. We show a few customization options with Matplotlib.
-
Example 5. We show how to use PIL and SciPy for some basic image processing algorithms.
-
Example 6. This is a 3D plot example with Matplotlib.
Chapter 5: High Performance and Parallel Computing
-
Example 1. We demonstrate the most basic interactive parallel computing features of IPython.
-
Example 2. We show how Monte Carlo simulations can be parallelized with IPython.
-
Example 3. This is a simple MPI example executed from an interactive IPython session.
-
Example 4. We show how an imperative algorithm (Sieve of Eratosthenes) implemented in Python can be accelerated with Cython.
-
Example 5. We show how to use Cython with NumPy-based algorithms for dramatic speed improvements.
Chapter 6: Customizing IPython
-
Example 1. We show how to load IPython extensions, with an application to line-by-line profiling of Python code.
-
Example 2. We show how to create a new IPython extension. As an example, we create a very basic extension for writing and executing C++ code in the IPython notebook.
-
Example 3. We illustrate the rich display features of the IPython notebook.