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

Preface

Whether you are new to scientific/analytic programming, or a seasoned expert, this book will provide you with the skills you need to successfully create, optimize, and distribute your Python/NumPy analytical modules.

Starting from the beginning, this book will cover the key features of NumPy arrays and the details of tuning the data format to make it most fit to your analytical needs. You will then get a walkthrough of the core and submodules that are common to various multidimensional, data-typed analysis. Next, you will move on to key technical implementations, such as linear algebra and Fourier analysis. Finally, you will learn about extending your NumPy capabilities for both functionality and performance by using Cython and the NumPy C API. The last chapter of this book also provides advanced materials to help you learn further by yourself.

This guide is an invaluable tutorial if you are planning to use NumPy in analytical projects.

What this book covers

Chapter 1, An Introduction to NumPy, is a Getting Started chapter of this book, which provides the instructions to help you set up the environment. It starts with introducing the Scientific Python Module family (SciPy Stack) and explains the key role NumPy plays in scientific computing with Python.

Chapter 2, The NumPy ndarray Object, covers the essential usage of NumPy ndarray object, including the initialization, the fundamental attributes, data types, and memory layout. It also covers the theory underneath the operation, which gives you a clear picture of ndarray.

Chapter 3, Using Numpy Arrays, is an advanced chapter on NumPy ndarray usage, which continues Chapter 2, The NumPy ndarray Object. It covers the universal functions in NumPy and shows you the tricks to speed up your code. It also shows you the shape manipulation and broadcasting rules.

Chapter 4, Numpy Core and Libs Submodules, includes two sections. The first section has detailed explanation about the relationship between the way NumPy ndarray allocates memory and the interaction of CPU cache. The second part of this chapter covers the special NumPy Array containing multiple data types (the structure/record array). Also, this chapter explores the experimental datetime64 module in NumPy.

Chapter 5, Linear Algebra in NumPy, starts by utilizing matrix and mathematical computation using linear algebra modules. It shows you multiple ways to solve a mathematical problem: using Matrix, vector decomposition, and polynomials. It also provides concrete practice for curve fitting and regression.

Chapter 6, Fourier Analysis in NumPy, covers the signal processing with NumPy FFT module and the Fourier application on amplifying signals/enlarging images without distortion. It also provides the basic usage of the matplotlib package in Python.

Chapter 7, Building and Distributing NumPy Code, covers the basic details around packaging and publishing the code in Python. It provides a basic introduction to NumPy-specific setup files and how to build extension modules.

Chapter 8, Speeding Up NumPy with Cython, introduces the users to the Cython programming language and introduces readers to techniques that can be used to speed up existing Python code.

Chapter 9, Introduction to the NumPy C-API, provides a basic introduction to the NumPy C API and, in general, how to write wrappers around the existing C/C++ library. The chapter aims to provide a gentle introduction along with equipping the readers with a basic knowledge of how to create new wrappers and understand the existing programs.

Chapter 10, Further Reading, is the last chapter of this book. It gives a summary of what we've learned in the book and explores 4 SciPy stack Python modules relying on NumPy arrays, which give you ideas about further scientific Python programming.

What you need for this book

For this book, you will need the following setup:

  • Python 2.x or 3.x
  • NumPy 1.9 (or later)
  • IPython Notebook
  • Matplotlib 1.3 (or later)
  • gnu gcc compiler or equivalent in Windows
  • setuptools

Who this book is for

If you know Python, but are new to scientific programming and want to enter the world of scientific computation, or perhaps you are a Python developer with experience in analytics, but want to gain insight to enhance your analytical skills. In either case, NumPy or this book is ideal for you. Learning NumPy and how to apply it to your Python programs is perfect as your next step towards building professional analytical applications. It would be helpful to have a bit of familiarity with basic programming concepts and mathematics, but no prior experience is required. The later chapters cover concepts such as package distribution, speeding-up code, and C/C++ integration, which require a certain amount of programming and debugging know-how. The readers are assumed to be able to build C/C++ programs in their preferred choice of OS (use gcc in linux and cygwin/migw and more in Windows).

Conventions

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

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Note that SciPy can mean a number of thing, like the Python module named scipy."

A block of code is set as follows:

In [42]: print("Hello, World!")

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

In [6]: x
Out[6]:
array([[1, 2, 3],
       [2, 3, 4]])
In [7]: x[0,0]
Out[7]: 1
In [8]: x[1,2]
Out[8]: 4

New terms and important words are shown in bold. 

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 disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

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 at 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 this book 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

Downloading the color images of this book 

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/NumPyEssentials_ColoredImages.pdf.

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 could 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/submit-errata, 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 to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted 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

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.