# IPython Cookbook

## IPython Cookbook, Second Edition (2018)

![](https://3650666907-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M16IoaANYRvc0I-U9uw%2F-M5XbfHUW3U1PU1quFgZ%2F-M5XfFXAhIK5DkE1rcua%2Fimage.png?alt=media\&token=46bc424d-4d03-41bc-b78c-7f6f2d0aaa7b)

Book : [https://ipython-books.github.io/](https://ipython-books.github.io/) \
SourceCode : [https://github.com/ipython-books/cookbook-2nd-code](https://github.com/ipython-books/cookbook-2nd-code)

**IPython Interactive Computing and Visualization Cookbook, Second Edition** (2018), by [Cyrille Rossant](http://cyrille.rossant.net/), contains over 100 hands-on recipes on high-performance numerical computing and data science in the Jupyter Notebook.

Most of the book is freely available on this website ([CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode)).

▶ [**Get the code** as Jupyter notebooks](https://github.com/ipython-books/cookbook-2nd-code)\
▶ [**Get the Google Chrome extension** to see LaTeX equations on GitHub](https://chrome.google.com/webstore/detail/github-with-mathjax/ioemnmodlmafdkllaclgeombjnmnbima/)\
▶ [**Buy the book**](https://www.packtpub.com/big-data-and-business-intelligence/ipython-interactive-computing-and-visualization-cookbook-second-e)

### Contents

#### [Chapter 1 : A Tour of Interactive Computing with Jupyter and IPython](https://ipython-books.github.io/chapter-1-a-tour-of-interactive-computing-with-jupyter-and-ipython/)

* [1.1. Introducing IPython and the Jupyter Notebook](https://ipython-books.github.io/11-introducing-ipython-and-the-jupyter-notebook/)
* [1.2. Getting started with exploratory data analysis in the Jupyter Notebook](https://ipython-books.github.io/12-getting-started-with-exploratory-data-analysis-in-the-jupyter-notebook/)
* [1.3. Introducing the multidimensional array in NumPy for fast array computations](https://ipython-books.github.io/13-introducing-the-multidimensional-array-in-numpy-for-fast-array-computations/)
* [1.4. Creating an IPython extension with custom magic commands](https://ipython-books.github.io/14-creating-an-ipython-extension-with-custom-magic-commands/)
* [1.5. Mastering IPython's configuration system](https://ipython-books.github.io/15-mastering-ipythons-configuration-system/)
* [1.6. Creating a simple kernel for Jupyter](https://ipython-books.github.io/16-creating-a-simple-kernel-for-jupyter/)

#### [Chapter 2 : Best practices in Interactive Computing](https://ipython-books.github.io/chapter-2-best-practices-in-interactive-computing/)

* [2.1. Learning the basics of the Unix shell](https://ipython-books.github.io/21-learning-the-basics-of-the-unix-shell/)
* [2.2. Using the latest features of Python 3](https://ipython-books.github.io/22-using-the-latest-features-of-python-3/)
* [2.3. Learning the basics of the distributed version control system Git](https://ipython-books.github.io/23-learning-the-basics-of-the-distributed-version-control-system-git/)
* [2.4. A typical workflow with Git branching](https://ipython-books.github.io/24-a-typical-workflow-with-git-branching/)
* [2.5. Efficient interactive computing workflows with IPython](https://ipython-books.github.io/25-efficient-interactive-computing-workflows-with-ipython/)
* [2.6. Ten tips for conducting reproducible interactive computing experiments](https://ipython-books.github.io/26-ten-tips-for-conducting-reproducible-interactive-computing-experiments/)
* [2.7. Writing high-quality Python code](https://ipython-books.github.io/27-writing-high-quality-python-code/)
* [2.8. Writing unit tests with py.test](https://ipython-books.github.io/28-writing-unit-tests-with-pytest/)
* [2.9. Debugging code with IPython](https://ipython-books.github.io/29-debugging-code-with-ipython/)  \*

#### [Chapter 3 : Mastering the Jupyter Notebook](https://ipython-books.github.io/chapter-3-mastering-the-jupyter-notebook/)

* [3.1. Teaching programming in the Notebook with IPython blocks](https://ipython-books.github.io/31-teaching-programming-in-the-notebook-with-ipython-blocks/)
* [3.2. Converting a Jupyter notebook to other formats with nbconvert](https://ipython-books.github.io/32-converting-a-jupyter-notebook-to-other-formats-with-nbconvert/)
* [3.3. Mastering widgets in the Jupyter Notebook](https://ipython-books.github.io/33-mastering-widgets-in-the-jupyter-notebook/)
* [3.4. Creating custom Jupyter Notebook widgets in Python, HTML, and JavaScript](https://ipython-books.github.io/34-creating-custom-jupyter-notebook-widgets-in-python-html-and-javascript/)
* [3.5. Configuring the Jupyter Notebook](https://ipython-books.github.io/35-configuring-the-jupyter-notebook/)  \*
* [3.6. Introducing JupyterLab](https://ipython-books.github.io/36-introducing-jupyterlab/)

#### [Chapter 4 : Profiling and Optimization](https://ipython-books.github.io/chapter-4-profiling-and-optimization/)

* [4.1. Evaluating the time taken by a command in IPython](https://ipython-books.github.io/41-evaluating-the-time-taken-by-a-command-in-ipython/)  \*
* [4.2. Profiling your code easily with cProfile and IPython](https://ipython-books.github.io/42-profiling-your-code-easily-with-cprofile-and-ipython/)
* [4.3. Profiling your code line-by-line with line\_profiler](https://ipython-books.github.io/43-profiling-your-code-line-by-line-with-line_profiler/)
* [4.4. Profiling the memory usage of your code with memory\_profiler](https://ipython-books.github.io/44-profiling-the-memory-usage-of-your-code-with-memory_profiler/)
* [4.5. Understanding the internals of NumPy to avoid unnecessary array copying](https://ipython-books.github.io/45-understanding-the-internals-of-numpy-to-avoid-unnecessary-array-copying/)
* [4.6. Using stride tricks with NumPy](https://ipython-books.github.io/46-using-stride-tricks-with-numpy/)
* [4.7. Implementing an efficient rolling average algorithm with stride tricks](https://ipython-books.github.io/47-implementing-an-efficient-rolling-average-algorithm-with-stride-tricks/)
* [4.8. Processing large NumPy arrays with memory mapping](https://ipython-books.github.io/48-processing-large-numpy-arrays-with-memory-mapping/)
* [4.9. Manipulating large arrays with HDF5](https://ipython-books.github.io/49-manipulating-large-arrays-with-hdf5/)  \*

#### [Chapter 5 : High-Performance Computing](https://ipython-books.github.io/chapter-5-high-performance-computing/)

* [5.1. Knowing Python to write faster code](https://ipython-books.github.io/51-knowing-python-to-write-faster-code/)
* [5.2. Accelerating pure Python code with Numba and just-in-time compilation](https://ipython-books.github.io/52-accelerating-pure-python-code-with-numba-and-just-in-time-compilation/)
* [5.3. Accelerating array computations with Numexpr](https://ipython-books.github.io/53-accelerating-array-computations-with-numexpr/)
* [5.4. Wrapping a C library in Python with ctypes](https://ipython-books.github.io/54-wrapping-a-c-library-in-python-with-ctypes/)
* [5.5. Accelerating Python code with Cython](https://ipython-books.github.io/55-accelerating-python-code-with-cython/)
* [5.6. Optimizing Cython code by writing less Python and more C](https://ipython-books.github.io/56-optimizing-cython-code-by-writing-less-python-and-more-c/)
* [5.7. Releasing the GIL to take advantage of multi-core processors with Cython and OpenMP](https://ipython-books.github.io/57-releasing-the-gil-to-take-advantage-of-multi-core-processors-with-cython-and-openmp/)
* [5.8. Writing massively parallel code for NVIDIA graphics cards (GPUs) with CUDA](https://ipython-books.github.io/58-writing-massively-parallel-code-for-nvidia-graphics-cards-gpus-with-cuda/)
* [5.9. Distributing Python code across multiple cores with IPython](https://ipython-books.github.io/59-distributing-python-code-across-multiple-cores-with-ipython/)
* [5.10. Interacting with asynchronous parallel tasks in IPython](https://ipython-books.github.io/510-interacting-with-asynchronous-parallel-tasks-in-ipython/)
* [5.11. Performing out-of-core computations on large arrays with Dask](https://ipython-books.github.io/511-performing-out-of-core-computations-on-large-arrays-with-dask/)
* [5.12. Trying the Julia programming language in the Jupyter Notebook](https://ipython-books.github.io/512-trying-the-julia-programming-language-in-the-jupyter-notebook/)  \*

#### [Chapter 6 : Data Visualization](https://ipython-books.github.io/chapter-6-data-visualization/)

* [6.1. Using matplotlib styles](https://ipython-books.github.io/61-using-matplotlib-styles/)
* [6.2. Creating statistical plots easily with seaborn](https://ipython-books.github.io/62-creating-statistical-plots-easily-with-seaborn/)
* [6.3. Creating interactive Web visualizations with Bokeh and HoloViews](https://ipython-books.github.io/63-creating-interactive-web-visualizations-with-bokeh-and-holoviews/)
* [6.4. Visualizing a NetworkX graph in the Notebook with D3.js](https://ipython-books.github.io/64-visualizing-a-networkx-graph-in-the-notebook-with-d3js/)
* [6.5. Discovering interactive visualization libraries in the Notebook](https://ipython-books.github.io/65-discovering-interactive-visualization-libraries-in-the-notebook/)  \*
* [6.6. Creating plots with Altair and the Vega-Lite specification](https://ipython-books.github.io/66-creating-plots-with-altair-and-the-vega-lite-specification/)

#### [Chapter 7 : Statistical Data Analysis](https://ipython-books.github.io/chapter-7-statistical-data-analysis/)

* [7.1. Exploring a dataset with pandas and matplotlib](https://ipython-books.github.io/71-exploring-a-dataset-with-pandas-and-matplotlib/)
* [7.2. Getting started with statistical hypothesis testing — a simple z-test](https://ipython-books.github.io/72-getting-started-with-statistical-hypothesis-testing-a-simple-z-test/)
* [7.3. Getting started with Bayesian methods](https://ipython-books.github.io/73-getting-started-with-bayesian-methods/)
* [7.4. Estimating the correlation between two variables with a contingency table and a chi-squared test](https://ipython-books.github.io/74-estimating-the-correlation-between-two-variables-with-a-contingency-table-and-a-chi-squared-test/)
* [7.5. Fitting a probability distribution to data with the maximum likelihood method](https://ipython-books.github.io/75-fitting-a-probability-distribution-to-data-with-the-maximum-likelihood-method/)
* [7.6. Estimating a probability distribution nonparametrically with a kernel density estimation](https://ipython-books.github.io/76-estimating-a-probability-distribution-nonparametrically-with-a-kernel-density-estimation/)
* [7.7. Fitting a Bayesian model by sampling from a posterior distribution with a Markov Chain Monte Carlo method](https://ipython-books.github.io/77-fitting-a-bayesian-model-by-sampling-from-a-posterior-distribution-with-a-markov-chain-monte-carlo-method/)
* [7.8. Analyzing data with the R programming language in the Jupyter Notebook](https://ipython-books.github.io/78-analyzing-data-with-the-r-programming-language-in-the-jupyter-notebook/)  \*

#### [Chapter 8 : Machine Learning](https://ipython-books.github.io/chapter-8-machine-learning/)

* [8.1. Getting started with scikit-learn](https://ipython-books.github.io/81-getting-started-with-scikit-learn/)
* [8.2. Predicting who will survive on the Titanic with logistic regression](https://ipython-books.github.io/82-predicting-who-will-survive-on-the-titanic-with-logistic-regression/)  \*
* [8.3. Learning to recognize handwritten digits with a K-nearest neighbors classifier](https://ipython-books.github.io/83-learning-to-recognize-handwritten-digits-with-a-k-nearest-neighbors-classifier/)
* [8.4. Learning from text — Naive Bayes for Natural Language Processing](https://ipython-books.github.io/84-learning-from-text-naive-bayes-for-natural-language-processing/)
* [8.5. Using support vector machines for classification tasks](https://ipython-books.github.io/85-using-support-vector-machines-for-classification-tasks/)
* [8.6. Using a random forest to select important features for regression](https://ipython-books.github.io/86-using-a-random-forest-to-select-important-features-for-regression/)
* [8.7. Reducing the dimensionality of a dataset with a principal component analysis](https://ipython-books.github.io/87-reducing-the-dimensionality-of-a-dataset-with-a-principal-component-analysis/)  \*
* [8.8. Detecting hidden structures in a dataset with clustering](https://ipython-books.github.io/88-detecting-hidden-structures-in-a-dataset-with-clustering/)

#### [Chapter 9 : Numerical Optimization](https://ipython-books.github.io/chapter-9-numerical-optimization/)

* [9.1. Finding the root of a mathematical function](https://ipython-books.github.io/91-finding-the-root-of-a-mathematical-function/)  \*
* [9.2. Minimizing a mathematical function](https://ipython-books.github.io/92-minimizing-a-mathematical-function/)
* [9.3. Fitting a function to data with nonlinear least squares](https://ipython-books.github.io/93-fitting-a-function-to-data-with-nonlinear-least-squares/)
* [9.4. Finding the equilibrium state of a physical system by minimizing its potential energy](https://ipython-books.github.io/94-finding-the-equilibrium-state-of-a-physical-system-by-minimizing-its-potential-energy/)

#### [Chapter 10 : Signal Processing](https://ipython-books.github.io/chapter-10-signal-processing/)

* [10.1. Analyzing the frequency components of a signal with a Fast Fourier Transform](https://ipython-books.github.io/101-analyzing-the-frequency-components-of-a-signal-with-a-fast-fourier-transform/)
* [10.2. Applying a linear filter to a digital signal](https://ipython-books.github.io/102-applying-a-linear-filter-to-a-digital-signal/)
* [10.3. Computing the autocorrelation of a time series](https://ipython-books.github.io/103-computing-the-autocorrelation-of-a-time-series/)

#### [Chapter 11 : Image and Audio Processing](https://ipython-books.github.io/chapter-11-image-and-audio-processing/)

* [11.1. Manipulating the exposure of an image](https://ipython-books.github.io/111-manipulating-the-exposure-of-an-image/)
* [11.2. Applying filters on an image](https://ipython-books.github.io/112-applying-filters-on-an-image/)
* [11.3. Segmenting an image](https://ipython-books.github.io/113-segmenting-an-image/)
* [11.4. Finding points of interest in an image](https://ipython-books.github.io/114-finding-points-of-interest-in-an-image/)
* [11.5. Detecting faces in an image with OpenCV](https://ipython-books.github.io/115-detecting-faces-in-an-image-with-opencv/)  \*
* [11.6. Applying digital filters to speech sounds](https://ipython-books.github.io/116-applying-digital-filters-to-speech-sounds/)
* [11.7. Creating a sound synthesizer in the Notebook](https://ipython-books.github.io/117-creating-a-sound-synthesizer-in-the-notebook/)

#### [Chapter 12 : Deterministic Dynamical Systems](https://ipython-books.github.io/chapter-12-deterministic-dynamical-systems/)

* [12.1. Plotting the bifurcation diagram of a chaotic dynamical system](https://ipython-books.github.io/121-plotting-the-bifurcation-diagram-of-a-chaotic-dynamical-system/)
* [12.2. Simulating an elementary cellular automaton](https://ipython-books.github.io/122-simulating-an-elementary-cellular-automaton/)
* [12.3. Simulating an ordinary differential equation with SciPy](https://ipython-books.github.io/123-simulating-an-ordinary-differential-equation-with-scipy/)
* [12.4. Simulating a partial differential equation — reaction-diffusion systems and Turing patterns](https://ipython-books.github.io/124-simulating-a-partial-differential-equation-reaction-diffusion-systems-and-turing-patterns/)

#### [Chapter 13 : Stochastic Dynamical Systems](https://ipython-books.github.io/chapter-13-stochastic-dynamical-systems/)

* [13.1. Simulating a discrete-time Markov chain](https://ipython-books.github.io/131-simulating-a-discrete-time-markov-chain/)
* [13.2. Simulating a Poisson process](https://ipython-books.github.io/132-simulating-a-poisson-process/)  \*
* [13.3. Simulating a Brownian motion](https://ipython-books.github.io/133-simulating-a-brownian-motion/)
* [13.4. Simulating a stochastic differential equation](https://ipython-books.github.io/134-simulating-a-stochastic-differential-equation/)

#### [Chapter 14 : Graphs, Geometry, and Geographic Information Systems](https://ipython-books.github.io/chapter-14-graphs-geometry-and-geographic-information-systems/)

* [14.1. Manipulating and visualizing graphs with NetworkX](https://ipython-books.github.io/141-manipulating-and-visualizing-graphs-with-networkx/)  \*
* [14.2. Drawing flight routes with NetworkX](https://ipython-books.github.io/142-drawing-flight-routes-with-networkx/)
* [14.3. Resolving dependencies in a directed acyclic graph with a topological sort](https://ipython-books.github.io/143-resolving-dependencies-in-a-directed-acyclic-graph-with-a-topological-sort/)
* [14.4. Computing connected components in an image](https://ipython-books.github.io/144-computing-connected-components-in-an-image/)
* [14.5. Computing the Voronoi diagram of a set of points](https://ipython-books.github.io/145-computing-the-voronoi-diagram-of-a-set-of-points/)
* [14.6. Manipulating geospatial data with Cartopy](https://ipython-books.github.io/146-manipulating-geospatial-data-with-cartopy/)
* [14.7. Creating a route planner for a road network](https://ipython-books.github.io/147-creating-a-route-planner-for-a-road-network/)

#### [Chapter 15 : Symbolic and Numerical Mathematics](https://ipython-books.github.io/chapter-15-symbolic-and-numerical-mathematics/)

* [15.1. Diving into symbolic computing with SymPy](https://ipython-books.github.io/151-diving-into-symbolic-computing-with-sympy/)
* [15.2. Solving equations and inequalities](https://ipython-books.github.io/152-solving-equations-and-inequalities/)
* [15.3. Analyzing real-valued functions](https://ipython-books.github.io/153-analyzing-real-valued-functions/)
* [15.4. Computing exact probabilities and manipulating random variables](https://ipython-books.github.io/154-computing-exact-probabilities-and-manipulating-random-variables/)
* [15.5. A bit of number theory with SymPy](https://ipython-books.github.io/155-a-bit-of-number-theory-with-sympy/)
* [15.6. Finding a Boolean propositional formula from a truth table](https://ipython-books.github.io/156-finding-a-boolean-propositional-formula-from-a-truth-table/)
* [15.7. Analyzing a nonlinear differential system — Lotka-Volterra (predator-prey) equations](https://ipython-books.github.io/157-analyzing-a-nonlinear-differential-system-lotka-volterra-predator-prey-equations/)
* [15.8. Getting started with Sage](https://ipython-books.github.io/158-getting-started-with-sage/)  \*

Recipes marked with an asterisk \* are only available in the [book](https://packtpub.com/big-data-and-business-intelligence/ipython-interactive-computing-and-visualization-cookbook-second-e).

### Contributing

For any comment, question, or error, please [open an issue](https://github.com/ipython-books/cookbook-2nd/issues) or [propose a pull request](https://github.com/ipython-books/cookbook-2nd/pulls).

### Presentation

Python is one of the leading open source platforms for data science and numerical computing. IPython and the associated Jupyter Notebook offer efficient interfaces to Python for data analysis and interactive visualization, and they constitute an ideal gateway to the platform.

IPython Interactive Computing and Visualization Cookbook, Second Edition contains many ready-to-use, focused recipes for high-performance scientific computing and data analysis, from the latest IPython/Jupyter features to the most advanced tricks, to help you write better and faster code. You will apply these state-of-the-art methods to various real-world examples, illustrating topics in applied mathematics, scientific modeling, and machine learning.

The first part of the book covers programming techniques: code quality and reproducibility, code optimization, high- performance computing through just-in-time 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
