Book Image

NumPy Beginner's Guide - Second Edition

By : Ivan Idris
Book Image

NumPy Beginner's Guide - Second Edition

By: Ivan Idris

Overview of this book

NumPy is an extension to, and the fundamental package for scientific computing with Python. In today's world of science and technology, it is all about speed and flexibility. When it comes to scientific computing, NumPy is on the top of the list. NumPy Beginner's Guide will teach you about NumPy, a leading scientific computing library. NumPy replaces a lot of the functionality of Matlab and Mathematica, but in contrast to those products, is free and open source. Write readable, efficient, and fast code, which is as close to the language of mathematics as is currently possible with the cutting edge open source NumPy software library. Learn all the ins and outs of NumPy that requires you to know basic Python only. Save thousands of dollars on expensive software, while keeping all the flexibility and power of your favourite programming language.You will learn about installing and using NumPy and related concepts. At the end of the book we will explore some related scientific computing projects. This book will give you a solid foundation in NumPy arrays and universal functions. Through examples, you will also learn about plotting with Matplotlib and the related SciPy project. NumPy Beginner's Guide will help you be productive with NumPy and have you writing clean and fast code in no time at all.
Table of Contents (19 chapters)
Numpy Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Scientists, engineers, and quantitative data analysts face many challenges nowadays. Data scientists want to be able to do numerical analysis of large datasets with minimal programming effort. They want to write readable, efficient, and fast code, which is as close as possible to the mathematical language package they are used to. A number of accepted solutions are available in the scientific computing world.

The C, C++, and Fortran programming languages have their benefits, but they are not interactive and considered too complex by many. The common commercial alternatives are amongst others, Matlab, Maple and Mathematica. These products provide powerful scripting languages, which are still more limited than any general purpose programming language. Other open source tools similar to Matlab exist such as R, GNU Octave, and Scilab. Obviously, they also lack the power of a language such as Python.

Python is a popular general-purpose programming language, widely used in the scientific community. You can access legacy C, Fortran, or R code easily from Python. It is object-oriented and considered more high level than C or Fortran. Python allows you to write readable and clean code with minimal fuss. However, it lacks a Matlab equivalent out of the box. That's where NumPy comes in. This book is about NumPy and related Python libraries such as SciPy and Matplotlib.

What is NumPy?

NumPy (from Numerical Python) is an open-source Python library for scientific computing. NumPy let's you work with arrays and matrices in a natural way. The library contains a long list of useful mathematical functions including some for linear algebra, Fourier transformation, and random number generation routines. LAPACK, a linear algebra library, is used by the NumPy linear algebra module (that is, if you have LAPACK installed on your system), otherwise, NumPy provides its own implementation. LAPACK is a well-known library originally written in Fortran on which Matlab relies as well. In a sense, NumPy replaces some of the functionality of Matlab and Mathematica, allowing rapid interactive prototyping.

We will not be discussing NumPy from a developing contributor perspective, but more from a user's perspective. NumPy is a very active project and has a lot of contributors. Maybe, one day you will be one of them!

History

NumPy is based on its predecessor Numeric. Numeric was first released in 1995 and has a deprecated status now. Neither Numeric nor NumPy made it into the standard Python library for various reasons. However, you can install NumPy separately as will be explained in Chapter 1, Numpy Quick Start.

In 2001, a number of people inspired by Numeric created SciPy—an open-source Python scientific computing library, that provides functionality similar to that of Matlab, Maple, and Mathematica. Around this time, people were growing increasingly unhappy with Numeric. Numarray was created as alternative to Numeric. Numarray was better in some areas than Numeric, but worked very differently. For that reason, SciPy kept on depending on the Numeric philosophy and the Numeric array object. As is customary with new "latest and greatest" software, the arrival of Numarray led to the development of an entire ecosystem around it with a range of useful tools.

In 2005, Travis Oliphant, an early contributor to SciPy, decided to do something about this situation. He tried to integrate some of the Numarray features into Numeric. A complete rewrite took place that culminated in the release of NumPy 1.0 in 2006. At this time, NumPy has all of the features of Numeric and Numarray and more. Upgrade tools are available to facilitate the upgrade from Numeric and Numarray. The upgrade is recommended since Numeric and Numarray are not actively supported any more.

Originally, the NumPy code was part of SciPy. It was later separated and is now used by SciPy for array and matrix processing.

Why use NumPy?

NumPy code is much cleaner than "straight" Python code that tries to accomplish the same task. There are less loops required, because operations work directly on arrays and matrices. The many convenience and mathematical functions make life easier as well. The underlying algorithms have stood the test of time and have been designed with high performance in mind.

NumPy's arrays are stored more efficiently than an equivalent data structure in base Python such as list of lists. Array IO is significantly faster too. The performance improvement scales with the number of elements of an array. For large arrays it really pays off to use NumPy. Files as large as several terabytes can be memory-mapped to arrays, leading to optimal reading and writing of data. The drawback of NumPy arrays is that they are more specialized than plain lists. Outside of the context of numerical computations, NumPy arrays are less useful. The technical details of NumPy arrays will be discussed in the later chapters.

Large portions of NumPy are written in C. That makes NumPy faster than pure Python code. A NumPy C API exists as well and it allows further extension of the functionality with the help of the C language of NumPy. The C API falls outside the scope of this book. Finally, since NumPy is open-source, you get all of the related advantages. The price is the lowest possible—free as in "beer". You don't have to worry about licenses every time somebody joins your team or you need an upgrade of the software. The source code is available to everyone. This of course is beneficial to the code quality.

Limitations of NumPy

If you are a Java programmer, you might be interested in Jython, the Java implementation of Python. In that case, I have bad news for you. Unfortunately, Jython runs on the Java Virtual Machine and cannot access NumPy, because NumPy's modules are mostly written in C. You could say that Jython and Python are two totally different worlds, although, they do implement the same specification. There are some workarounds for this that are discussed in NumPy Cookbook, Ivan Idris, Packt Publishing.

What this book covers

Chapter 1, NumPy Quick Start will guide you through the steps needed to install NumPy on your system and create a basic NumPy application.

Chapter 2, Beginning with NumPy Fundamentals introduces you to NumPy arrays and fundamentals.

Chapter 3, Get to Terms with Commonly Used Functions will teach you about the most commonly used NumPy functions—the basic mathematical and statistical functions.

Chapter 4, Convenience Functions for Your Convenience will teach you about functions that make working with NumPy easier. This includes functions that select certain parts of your arrays, for instance, based on a Boolean condition. You will also learn about polynomials, and manipulating the shape of NumPy objects.

Chapter 5, Working with Matrices and ufuncs covers matrices and universal functions. Matrices are well known in mathematics and have their representation in NumPy as well. Universal functions (ufuncs) work on arrays element-by-element or on scalars. Ufuncs expect a set of scalars as input and produce a set of scalars as output.

Chapter 6, Move Further with Numpy Modules discusses the number of basic modules of Universal functions. Universal functions can typically be mapped to mathematical counterparts such as add, subtract, divide, and multiply.

Chapter 7, Peeking into Special Routines describes some of the more specialized NumPy functions. As NumPy users, we sometimes find ourselves having special needs. Fortunately, NumPy provides for most of our needs.

In Chapter 8, Assure Quality with Testing you will learn how to write NumPy unit tests.

Chapter 9, Plotting with Matplotlib covers in-depth Matplotlib, a very useful Python plotting library. NumPy on its own cannot be used to create graphs and plots. But Matplotlib integrates nicely with NumPy and has plotting capabilities comparable to Matlab.

Chapter 10, When NumPy is Not Enough – SciPy and Beyond goes into more detail about SciPy, we know that SciPy and NumPy are historically related. SciPy, as mentioned in the History section, is a high level Python scientific computing framework built on top of NumPy. It can be used in conjunction with NumPy.

Chapter 11, Playing with Pygame is the dessert of this book. We will learn how to create fun games with NumPy and Pygame. We also get a taste of artificial intelligence.

What you need for this book

To try out the code samples in this book you will need a recent build of NumPy. This means that you will need to have one of the Python versions supported by NumPy as well. Some code samples make use of the Matplotlib for illustration purposes. Matplotlib is not strictly required to follow the examples, but it is recommended that you install it too. The last chapter is about SciPy and has one example involving Scikits.

Here is a list of software used to develop and test the code examples:

  • Python 2.7

  • NumPy 2.0.0.dev20100915

  • SciPy 0.9.0.dev20100915

  • Matplotlib 1.1.1

  • Pygame 1.9.1

  • IPython 0.14.dev

Needless to say, you don't need to have exactly this software and these versions on your computer. Python and NumPy is the absolute minimum you will need.

Who this book is for

This book is for you the scientist, engineer, programmer, or analyst, looking for a high quality open source mathematical library. Knowledge of Python is assumed. Also, some affinity or at least interest in mathematics and statistics is required.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Notice that numpysum() does not need a for loop."

A block of code is set as follows:

def numpysum(n):
  a = numpy.arange(n) ** 2
  b = numpy.arange(n) ** 3
  c = a + b
  return c

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

reals = np.isreal(xpoints)
print "Real number?", reals
Real number? [ True  True  True  True False False False False]

Any command-line input or output is written as follows:

>>>fromnumpy.testing import rundocs
>>>rundocs('docstringtest.py')

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.