Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Congratulations on purchasing Learning NumPy Array! This was a smart investment, which is guaranteed to save you a lot of time Googling and searching through (online) documentation. You will learn all the essential things needed to become a confident NumPy user. NumPy started originally as part of SciPy and then was singled out as a fundamental library, which other open source Python APIs build on. As such, it is a crucial part of the common Python stack used for numerical and data analysis.

NumPy code is much cleaner than "straight" Python code that tries to accomplish the same task. There are fewer loops required, because operations work directly on arrays and matrices. The many conveniences 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 in an equivalent data structure in base Python, such as in a 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.

Large portions of NumPy are written in C. That makes NumPy faster than pure Python code. 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.

What this book covers

Chapter 1, Getting Started with NumPy, will guide you through the steps needed to install NumPy on your system and helps you create a basic NumPy application. We also successfully run a vector addition program.

Chapter 2, NumPy Basics, introduces you to NumPy arrays and fundamentals. In this chapter, we also learn that NumPy arrays can be sliced and indexed in an efficient manner. Here, we will understand the manipulation of shapes of various arrays.

Chapter 3, Basic Data Analysis with NumPy, tells us about learning data analysis with weather data analysis as an example. We will also explore the data from a KNMI weather station.

Chapter 4, Simple Predictive Analytics with NumPy, helps us attempt to predict the weather with simple models, such as Autoregressive Model with Lag 1 and Autoregressive Model with Lag 2.

Chapter 5, Signal Processing Techniques, gives us examples of signal processing and time series analysis. We look at smoothing with window functions and moving averages. We also touch upon the sifting process used by scientists to derive sunspot cycles. And we also get a demonstration of cointegration.

Chapter 6, Profiling, Debugging, and Testing, is about profiling, debugging, and testing, which are essential phases in the development cycle. We also cover unit testing, assert functions, and floating-point precision in depth.

Chapter 7, The Scientific Python Ecosystem, gives an overview of the Python ecosystem in which NumPy takes a central role. We also examine Cython, which is a relatively young programming language based on Python. We also have a look at Clustering, a type of machine learning algorithm.

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.

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

  • Python 2.7

  • Cython-0.17-py2.7-macosx-10.8-intel.egg

  • ipython-2.0.0_dev-py2.7.egg

  • matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg

  • numpy-1.9.0.dev_e886943-py2.7-macosx-10.9-intel.egg

  • pandas-0.8.2.dev_f5a74d4_20120725-py2.7-macosx-10.8-x86_64.egg

  • pip-1.4.1-py2.7.egg

  • statsmodels-0.6.0-py2.7-macosx-10.9-intel.egg

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 a scientist, engineer, programmer or an 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: "In this example, we chose numpy-1.8.0-win32-superpack-python2.7.exe."

A block of code is set as follows:

start = datetime.now()
c = pythonsum(size)
delta = datetime.now() - start
print "The last 2 elements of the sum", c[-2:]
print "PythonSum elapsed time in microseconds", delta.microseconds

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

$ python vectorsum.py 4000

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: "Click on the Continue button."

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 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.