Book Image

IPython Interactive Computing and Visualization Cookbook

By : Cyrille Rossant
Book Image

IPython Interactive Computing and Visualization Cookbook

By: Cyrille Rossant

Overview of this book

Table of Contents (22 chapters)
IPython Interactive Computing and Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Accelerating array computations with Numexpr


Numexpr is a package that improves upon a weakness of NumPy; the evaluation of complex array expressions is sometimes slow. The reason is that multiple temporary arrays are created for the intermediate steps, which is suboptimal with large arrays. Numexpr evaluates algebraic expressions involving arrays, parses them, compiles them, and finally executes them faster than NumPy.

This principle is somewhat similar to Numba, in that normal Python code is compiled dynamically by a JIT compiler. However, Numexpr only tackles algebraic array expressions rather than arbitrary Python code. We will see how that works in this recipe.

Getting ready

You will find the instructions to install Numexpr in the documentation available at http://github.com/pydata/numexpr.

How to do it…

  1. Let's import NumPy and Numexpr:

    In [1]: import numpy as np
            import numexpr as ne
  2. Then, we generate three large vectors:

    In [2]: x, y, z = np.random.rand(3, 1000000)
  3. Now, we evaluate...