Book Image

NumPy Essentials

By : Leo (Liang-Huan) Chin, Tanmay Dutta, Shane Holloway
Book Image

NumPy Essentials

By: Leo (Liang-Huan) Chin, Tanmay Dutta, Shane Holloway

Overview of this book

In today’s world of science and technology, it’s all about speed and flexibility. When it comes to scientific computing, NumPy tops the list. NumPy gives you both the speed and high productivity you need. This book will walk you through NumPy using clear, step-by-step examples and just the right amount of theory. We will guide you through wider applications of NumPy in scientific computing and will then focus on the fundamentals of NumPy, including array objects, functions, and matrices, each of them explained with practical examples. You will then learn about different NumPy modules while performing mathematical operations such as calculating the Fourier Transform; solving linear systems of equations, interpolation, extrapolation, regression, and curve fitting; and evaluating integrals and derivatives. We will also introduce you to using Cython with NumPy arrays and writing extension modules for NumPy code using the C API. This book will give you exposure to the vast NumPy library and help you build efficient, high-speed programs using a wide range of mathematical features.
Table of Contents (16 chapters)
NumPy Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface

The scientific Python stack


Let's begin by taking a brief tour of the Scientific Python (SciPy) stack.

Note

Note that SciPy can mean a number of things: the Python module named scipy (http://www.scipy.org/scipylib), the entire SciPy stack (http://www.scipy.org/about.html), or any of the three conferences on scientific Python that take place all over the world.

Figure 1: The SciPy stack, standard, and extended libraries

Fernando Perez, the primary author of IPython, said in his keynote at PyCon, Canada 2012:

"Computing in science has evolved not only because software has evolved, but also because we, as scientists, are doing much more than just floating point arithmetic."

This is precisely why the SciPy stack boasts such rich functionality. The evolution of most of the SciPy stack is motivated by teams of scientists and engineers trying to solve scientific and engineering problems in a general-purpose programming language. A one-line explanation of why NumPy matters so much is that it provides the core multidimensional array object that is necessary for most tasks in scientific computing. This is why it is at the root of the SciPy stack. NumPy provides an easy way to interface with legacy Fortran and C/C++ numerical code using time-tested scientific libraries, which we know have been working well for decades. Companies and labs across the world use Python to glue together legacy code that has been around for a long time. In short, this means that NumPy allows us to stand on the shoulders of giants; we do not have to reinvent the wheel. It is a dependency for every other SciPy package. The NumPy ndarray object, which is the subject of the next chapter, is essentially a Pythonic interface to data structures used by libraries written in Fortran, C, and, C++. In fact, the internal memory layouts used by NumPy ndarray objects implement C and Fortran layouts. This will be addressed in detail in upcoming chapters.

The next layer in the stack consists of SciPy, matplotlib, IPython (the interactive shell of Python; we will use it for the examples throughout the book, and details of its installation and usage will be provided in later sections), and SymPy modules. SciPy provides the bulk of the scientific and numerical functionality that a major part of the ecosystem relies on. Matplotlib is the de facto plotting and data visualization library in Python. IPython is an increasingly popular interactive environment for scientific computing in Python. In fact, the project has had such active development and enjoyed such popularity that it is no longer limited to Python and extends its features to other scientific languages, particularly R and Julia. This layer in the stack can be thought of as a bridge between the core array-oriented functionality of NumPy and the domain-specific abstractions provided by the higher layers of the stack. These domain-specific tools are commonly called SciKits-popular ones among them are scikit-image (image processing), scikit-learn (machine learning), statsmodels (statistics), pandas (advanced data analysis), and so on. Listing every scientific package in Python would be nearly impossible since the scientific Python community is very active, and there is always a lot of development happening for a large number of scientific problems. The best way to keep track of projects is to get involved in the community. It is immensely useful to join mailing lists, contribute to code, use the software for your daily computational needs, and report bugs. One of the goals of this book is to get you interested enough to actively involve yourself in the scientific Python community.