O'Reilly logo

SciPy and NumPy by Eli Bressert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Introduction

Python is a powerful programming language when considering portability, flexibility, syntax, style, and extendability. The language was written by Guido van Rossum with clean syntax built in. To define a function or initiate a loop, indentation is used instead of brackets. The result is profound: a Python programmer can look at any given uncommented Python code and quickly understand its inner workings and purpose.

Compiled languages like Fortran and C are natively much faster than Python, but not necessarily so when Python is bound to them. Using packages like Cython enables Python to interface with C code and pass information from the C program to Python and vice versa through memory. This allows Python to be on par with the faster languages when necessary and to use legacy code (e.g., FFTW). The combination of Python with fast computation has attracted scientists and others in large numbers. Two packages in particular are the powerhouses of scientific Python: NumPy and SciPy. Additionally, these two packages makes integrating legacy code easy.

1.1 Why SciPy and NumPy?

The basic operations used in scientific programming include arrays, matrices, integration, differential equation solvers, statistics, and much more. Python, by default, does not have any of these functionalities built in, except for some basic mathematical operations that can only deal with a variable and not an array or matrix. NumPy and SciPy are two powerful Python packages, however, that enable the language to be used efficiently for scientific purposes.

NumPy specializes in numerical processing through multi-dimensional ndarrays, where the arrays allow element-by-element operations, a.k.a. broadcasting. If needed, linear algebra formalism can be used without modifying the NumPy arrays beforehand. Moreover, the arrays can be modified in size dynamically. This takes out the worries that usually mire quick programming in other languages. Rather than creating a new array when you want to get rid of certain elements, you can apply a mask to it.

SciPy is built on the NumPy array framework and takes scientific programming to a whole new level by supplying advanced mathematical functions like integration, ordinary differential equation solvers, special functions, optimizations, and more. To list all the functions by name in SciPy would take several pages at minimum. When looking at the plethora of SciPy tools, it can sometimes be daunting even to decide which functions are best to use. That is why this book has been written. We will run through the primary and most often used tools, which will enable the reader to get results quickly and to explore the NumPy and SciPy packages with enough working knowledge to decide what is needed for problems that go beyond this book.

1.2 Getting NumPy and SciPy

Now you’re probably sold and asking, “Great, where can I get and install these packages?” There are multiple ways to do this, and we will first go over the easiest ways for OS X, Linux, and Windows.

There are three well-known, comprehensive, precompiled Python packages that include NumPy and SciPy, and that work on all three platforms: Continuum’s Anaconda distribution, Enthought Python Distribution (EPD), and ActivePython (AP). If you would like the free versions of the these packages, you should download Anaconda, EPD Free, or AP Community Edition. If you need support, then you can always opt for the more comprehensive packages.

Optionally, if you are a MacPorts user, you can install NumPy and SciPy through the package manager. Use the MacPorts command as given below to install the Python packages. Note that installing SciPy and NumPy with MacPorts will take time, especially with the SciPy package, so it’s a good idea to initiate the installation procedure and go grab a cup of tea:

sudo port install py27-numpy py27-scipy py27-ipython

MacPorts supports several versions of Python (e.g., 2.6 and 2.7). So, although py27 is listed above, if you would like to use Python 2.6 instead, then you would simply replace py27 with py26.

If you’re using a Debian-based Linux distro like Ubuntu or Linux Mint, then use apt-get to install the packages:

sudo apt-get install python-numpy python-scipy

With an RPM-based system like Fedora or OpenSUSE, you can install the Python packages using yum:

sudo yum install numpy scipy

Building and installing NumPy and SciPy on Windows systems is more complicated than on the Unix-based systems, as code compilation is tricky. Fortunately, there is an excellent compiled binary installation program called python(x,y) that includes both NumPy and SciPy and is Windows-specific.

For those who prefer building NumPy and SciPy from source, download the code from either the stable or bleeding-edge repositories. You can also clone the code repositories from the SciPy GitHub and the NumPy GitHub. Unless you’re a pro at building packages from source code and relish the challenge, though, I would recommend sticking with the precompiled package options listed above.

1.3 Working with SciPy and NumPy

You can work with Python programs in two different ways: interactively or through scripts. Some programmers swear that it is best to script all your code, so you don’t have to redo tedious tasks again when needed. Others say that interactive programming is the way to go, as you can explore the functionalities inside out. I would vouch for both, personally. If you have a terminal with the Python environment open and a text editor to write your script, you get the best of both worlds.

For the interactive component, I highly recommend IPython. It takes the best of the bash environment (e.g., using the Tab button to complete a command and changing directories) and combines it with the Python environment. It does far more than this, but for the purpose of the examples in this book it should be enough to get it up and running.


Bugs in programs are a fact of life and there’s no way around them. Being able to find bugs and fix them quickly and easily is a big part of successful programming. IPython contains a feature where you can debug a buggy Python script by typing debug after running it. See the IPython tutorial page for details under the debugging section.

1.4 Source Code

The code discussed in this book is provided in text format for easy reproduction and hacking. It includes the commands to make the plots, which are based on the Matplotlib package. Hence, some basic knowledge of the package will be required to understand the graphical commands.

As this book proceeds through new editions, new code examples will be added to this repository. Additionally, any changes in the API of the Python packages will be factored in as the updates are published.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required