You are previewing IPython Interactive Computing and Visualization Cookbook.
O'Reilly logo
IPython Interactive Computing and Visualization Cookbook

Book Description

Over 100 hands-on recipes to sharpen your skills in high-performance numerical computing and data science with Python

In Detail

IPython is at the heart of the Python scientific stack. With its widely acclaimed web-based notebook, IPython is today an ideal gateway to data analysis and numerical computing in Python.

IPython Interactive Computing and Visualization Cookbook contains many ready-to-use focused recipes for high-performance scientific computing and data analysis. The first part covers programming techniques, including code quality and reproducibility; code optimization; high-performance computing through dynamic compilation, parallel computing, and graphics card programming. The second part tackles data science, statistics, machine learning, signal and image processing, dynamical systems, and pure and applied mathematics.

What You Will Learn

  • Code better by writing high-quality, readable, and well-tested programs; profiling and optimizing your code, and conducting reproducible interactive computing experiments
  • Master all of the new features of the IPython notebook, including the interactive HTML/JavaScript widgets
  • Analyze data with Bayesian and frequentist statistics (Pandas, PyMC, and R), and learn from data with machine learning (scikit-learn)
  • Gain valuable insights into signals, images, and sounds with SciPy, scikit-image, and OpenCV
  • Learn how to write blazingly fast Python programs with NumPy, PyTables, ctypes, Numba, Cython, OpenMP, GPU programming (CUDA and OpenCL), parallel IPython, MPI, and many more
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. IPython Interactive Computing and Visualization Cookbook
      1. Table of Contents
      2. IPython Interactive Computing and Visualization Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book is
        2. What this book covers
          1. Part 1 – Advanced High-Performance Interactive Computing
          2. Part 2 – Standard Methods in Data Science and Applied Mathematics
        3. What you need for this book
          1. Installing Python
          2. GitHub repositories
        4. Who this book is for
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Downloading the color images
          3. Errata
          4. Piracy
          5. Questions
      8. 1. A Tour of Interactive Computing with IPython
        1. Introduction
          1. What is IPython?
          2. A brief historical retrospective on Python as a scientific environment
          3. What's new in IPython 2.0?
          4. Roadmap for IPython 3.0 and 4.0
          5. References
        2. Introducing the IPython notebook
          1. Getting ready
          2. How to do it...
          3. There's more...
          4. See also
        3. Getting started with exploratory data analysis in IPython
          1. How to do it...
          2. There's more...
          3. See also
        4. Introducing the multidimensional array in NumPy for fast array computations
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Creating an IPython extension with custom magic commands
          1. How to do it...
          2. How it works...
            1. The InteractiveShell class
            2. Loading an extension
          3. There's more...
          4. See also
        6. Mastering IPython's configuration system
          1. How to do it...
          2. How it works...
            1. Configurables
            2. Magics
          3. There's more...
          4. See also
        7. Creating a simple kernel for IPython
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      9. 2. Best Practices in Interactive Computing
        1. Introduction
        2. Choosing (or not) between Python 2 and Python 3
          1. How to do it...
            1. Main differences in Python 3 compared to Python 2
            2. Python 2 or Python 3?
            3. Supporting both Python 2 and Python 3
            4. Using 2to3
            5. Writing code that works in Python 2 and Python 3
          2. There's more...
          3. See also
        3. Efficient interactive computing workflows with IPython
          1. How to do it...
            1. The IPython terminal
            2. IPython and text editor
            3. The IPython notebook
            4. Integrated Development Environments
          2. There's more...
          3. See also
        4. Learning the basics of the distributed version control system Git
          1. Getting ready
          2. How to do it…
            1. Creating a local repository
            2. Cloning a remote repository
          3. How it works…
          4. There's more…
          5. See also
        5. A typical workflow with Git branching
          1. Getting ready
          2. How to do it…
            1. Stashing
          3. How it works…
          4. There's more…
          5. See also
        6. Ten tips for conducting reproducible interactive computing experiments
          1. How to do it…
          2. How it works…
          3. There's more...
          4. See also
        7. Writing high-quality Python code
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        8. Writing unit tests with nose
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Test coverage
            2. Workflows with unit testing
            3. Unit testing and continuous integration
        9. Debugging your code with IPython
          1. How to do it...
            1. The post-mortem mode
            2. Step-by-step debugging
          2. There's more...
            1. GUI debuggers
      10. 3. Mastering the Notebook
        1. Introduction
          1. What is the notebook?
          2. The notebook ecosystem
          3. Architecture of the IPython notebook
            1. Connecting multiple clients to one kernel
          4. Security in notebooks
          5. References
        2. Teaching programming in the notebook with IPython blocks
          1. Getting ready
          2. How to do it...
          3. There's more...
        3. Converting an IPython notebook to other formats with nbconvert
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Adding custom controls in the notebook toolbar
          1. How to do it...
          2. There's more...
          3. See also
        5. Customizing the CSS style in the notebook
          1. Getting ready
          2. How to do it...
          3. There's more...
          4. See also
        6. Using interactive widgets – a piano in the notebook
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Creating a custom JavaScript widget in the notebook – a spreadsheet editor for pandas
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Processing webcam images in real time from the notebook
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      11. 4. Profiling and Optimization
        1. Introduction
        2. Evaluating the time taken by a statement in IPython
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        3. Profiling your code easily with cProfile and IPython
          1. How to do it...
          2. How it works...
            1. "Premature optimization is the root of all evil"
          3. There's more...
          4. See also
        4. Profiling your code line-by-line with line_profiler
          1. Getting ready
          2. How do to it...
          3. How it works...
          4. There's more...
            1. Tracing the step-by-step execution of a Python program
          5. See also
        5. Profiling the memory usage of your code with memory_profiler
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using memory_profiler for standalone Python programs
            2. Using the %memit magic command in IPython
            3. Other tools
          5. See also
        6. Understanding the internals of NumPy to avoid unnecessary array copying
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Why are NumPy arrays efficient?
            2. What is the difference between in-place and implicit-copy operations?
            3. Why can't some arrays be reshaped without a copy?
            4. What are NumPy broadcasting rules?
          4. There's more...
          5. See also
        7. Using stride tricks with NumPy
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Implementing an efficient rolling average algorithm with stride tricks
          1. Getting ready
          2. How to do it...
          3. See also
        9. Making efficient array selections in NumPy
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Processing huge NumPy arrays with memory mapping
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        11. Manipulating large arrays with HDF5 and PyTables
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        12. Manipulating large heterogeneous tables with HDF5 and PyTables
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      12. 5. High-performance Computing
        1. Introduction
          1. CPython and concurrent programming
          2. Compiler-related installation instructions
            1. Linux
            2. Mac OS X
            3. Windows
              1. Python 32-bit
              2. Python 64-bit
              3. DLL hell
              4. References
        2. Accelerating pure Python code with Numba and just-in-time compilation
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Accelerating array computations with Numexpr
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. See also
        4. Wrapping a C library in Python with ctypes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Accelerating Python code with Cython
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Optimizing Cython code by writing less Python and more C
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        7. Releasing the GIL to take advantage of multicore processors with Cython and OpenMP
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Writing massively parallel code for NVIDIA graphics cards (GPUs) with CUDA
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. There's more…
          5. See also
        9. Writing massively parallel code for heterogeneous platforms with OpenCL
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        10. Distributing Python code across multiple cores with IPython
          1. How to do it…
          2. How it works…
          3. There's more…
            1. Dependent parallel tasks
            2. Alternative parallel computing solutions
            3. References
          4. See also
        11. Interacting with asynchronous parallel tasks in IPython
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        12. Parallelizing code with MPI in IPython
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        13. Trying the Julia language in the notebook
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      13. 6. Advanced Visualization
        1. Introduction
        2. Making nicer matplotlib figures with prettyplotlib
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Creating beautiful statistical plots with seaborn
          1. Getting ready
          2. How to do it…
          3. There's more…
          4. See also
        4. Creating interactive web visualizations with Bokeh
          1. Getting ready
          2. How to do it…
          3. There's more…
          4. See also
        5. Visualizing a NetworkX graph in the IPython notebook with D3.js
          1. Getting ready
          2. How to do it…
          3. There's more…
          4. See also
        6. Converting matplotlib figures to D3.js visualizations with mpld3
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        7. Getting started with Vispy for high-performance interactive data visualizations
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Vispy for scientific visualization
      14. 7. Statistical Data Analysis
        1. Introduction
          1. What is statistical data analysis?
          2. A bit of vocabulary
            1. Exploration, inference, decision, and prediction
            2. Univariate and multivariate methods
            3. Frequentist and Bayesian methods
            4. Parametric and nonparametric inference methods
        2. Exploring a dataset with pandas and matplotlib
          1. Getting ready
          2. How to do it...
          3. There's more...
        3. Getting started with statistical hypothesis testing – a simple z-test
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Getting started with Bayesian methods
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Bayes' theorem
            2. Computation of the posterior distribution
            3. Maximum a posteriori estimation
          4. There's more...
            1. Credible interval
            2. Conjugate distributions
            3. Non-informative (objective) prior distributions
          5. See also
        5. Estimating the correlation between two variables with a contingency table and a chi-squared test
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Pearson's correlation coefficient
            2. Contingency table and chi-squared test
          4. There's more...
          5. See also
        6. Fitting a probability distribution to data with the maximum likelihood method
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Estimating a probability distribution nonparametrically with a kernel density estimation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Fitting a Bayesian model by sampling from a posterior distribution with a Markov chain Monte Carlo method
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Analyzing data with the R programming language in the IPython notebook
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      15. 8. Machine Learning
        1. Introduction
          1. A bit of vocabulary
            1. Learning from data
            2. Supervised learning
            3. Unsupervised learning
            4. Feature selection and feature extraction
            5. Overfitting, underfitting, and the bias-variance tradeoff
            6. Model selection
            7. Machine learning references
        2. Getting started with scikit-learn
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. The scikit-learn API
            2. Ordinary least squares regression
            3. Polynomial interpolation with linear regression
            4. Ridge regression
            5. Cross-validation and grid search
          4. There's more…
          5. See also
        3. Predicting who will survive on the Titanic with logistic regression
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Learning to recognize handwritten digits with a K-nearest neighbors classifier
          1. How to do it...
          2. How it works...
          3. There's more…
          4. See also
        5. Learning from text – Naive Bayes for Natural Language Processing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
          5. See also
        6. Using support vector machines for classification tasks
          1. How to do it...
          2. How it works...
          3. There's more…
          4. See also
        7. Using a random forest to select important features for regression
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        8. Reducing the dimensionality of a dataset with a principal component analysis
          1. How to do it...
          2. How it works...
          3. There's more…
          4. See also
        9. Detecting hidden structures in a dataset with clustering
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      16. 9. Numerical Optimization
        1. Introduction
          1. The objective function
          2. Local and global minima
          3. Constrained and unconstrained optimization
          4. Deterministic and stochastic algorithms
          5. References
        2. Finding the root of a mathematical function
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        3. Minimizing a mathematical function
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        4. Fitting a function to data with nonlinear least squares
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        5. Finding the equilibrium state of a physical system by minimizing its potential energy
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
      17. 10. Signal Processing
        1. Introduction
          1. Analog and digital signals
          2. The Nyquist–Shannon sampling theorem
          3. Compressed sensing
          4. References
        2. Analyzing the frequency components of a signal with a Fast Fourier Transform
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. The Discrete Fourier Transform
            2. Inverse Fourier Transform
          4. There's more...
          5. See also
        3. Applying a linear filter to a digital signal
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. What are linear filters?
            2. Linear filters and convolutions
            3. The FIR and IIR filters
            4. Filters in the frequency domain
            5. The low-, high-, and band-pass filters
          4. There's more...
          5. See also
        4. Computing the autocorrelation of a time series
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
      18. 11. Image and Audio Processing
        1. Introduction
          1. Images
          2. Sounds
          3. References
        2. Manipulating the exposure of an image
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Applying filters on an image
          1. How it works...
          2. How it works...
          3. There's more...
          4. See also
        4. Segmenting an image
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Finding points of interest in an image
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Detecting faces in an image with OpenCV
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Applying digital filters to speech sounds
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more...
          5. See also
        8. Creating a sound synthesizer in the notebook
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      19. 12. Deterministic Dynamical Systems
        1. Introduction
          1. Types of dynamical systems
          2. Differential equations
          3. References
        2. Plotting the bifurcation diagram of a chaotic dynamical system
          1. How to do it...
          2. There's more...
          3. See also
        3. Simulating an elementary cellular automaton
          1. How to do it...
          2. How it works...
          3. There's more...
        4. Simulating an ordinary differential equation with SciPy
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Simulating a partial differential equation – reaction-diffusion systems and Turing patterns
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      20. 13. Stochastic Dynamical Systems
        1. Introduction
          1. References
        2. Simulating a discrete-time Markov chain
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        3. Simulating a Poisson process
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        4. Simulating a Brownian motion
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Simulating a stochastic differential equation
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      21. 14. Graphs, Geometry, and Geographic Information Systems
        1. Introduction
          1. Graphs
            1. Problems in graph theory
            2. Random graphs
            3. Graphs in Python
          2. Geometry in Python
          3. Geographical Information Systems in Python
          4. References
        2. Manipulating and visualizing graphs with NetworkX
          1. Getting ready
          2. How to do it…
          3. There's more…
          4. See also
        3. Analyzing a social network with NetworkX
          1. Getting ready
          2. How to do it…
          3. There's more…
          4. See also
        4. Resolving dependencies in a directed acyclic graph with a topological sort
          1. Getting ready
          2. How to do it…
          3. There's more…
        5. Computing connected components in an image
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Computing the Voronoi diagram of a set of points
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        7. Manipulating geospatial data with Shapely and basemap
          1. Getting ready
          2. How to do it…
          3. See also
        8. Creating a route planner for a road network
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      22. 15. Symbolic and Numerical Mathematics
        1. Introduction
          1. LaTeX
        2. Diving into symbolic computing with SymPy
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Solving equations and inequalities
          1. Getting ready
          2. How to do it...
          3. There's more...
        4. Analyzing real-valued functions
          1. Getting ready
          2. How to do it...
          3. There's more...
        5. Computing exact probabilities and manipulating random variables
          1. How to do it...
          2. How it works...
        6. A bit of number theory with SymPy
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Finding a Boolean propositional formula from a truth table
          1. How to do it...
          2. How it works...
          3. There's more...
        8. Analyzing a nonlinear differential system – Lotka-Volterra (predator-prey) equations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Getting started with Sage
          1. Getting ready
          2. How to do it...
          3. There's more...
          4. See also
      23. Index