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

Chapter 1. An Introduction to NumPy

 

"I'd rather do math in a general-purpose language than try to do general-purpose programming in a math language."                                                                                                                     

 
 -- John D Cook

Python has become one of the most popular programming languages in scientific computing over the last decade. The reasons for its success are numerous, and these will gradually become apparent as you proceed with this book. Unlike many other mathematical languages, such as MATLAB, R and Mathematica, Python is a general-purpose programming language. As such, it provides a suitable framework to build scientific applications and extend them further into any commercial or academic domain. For example, consider a (somewhat) simple application that requires you to write a piece of software and predicts the popularity of a blog post. Usually, these would be the steps that you'd take to do this:

  1. Generating a corpus of blog posts and their corresponding ratings (assuming that the ratings here are suitably quantifiable).
  2. Formulating a model that generates ratings based on content and other data associated with the blog post.
  3. Training a model on the basis of the data you found in step 1. Keep doing this until you are confident of the reliability of the model.
  4. Deploying the model as a web service.

Normally, as you move through these steps, you will find yourself jumping between different software stacks. Step 1 requires a lot of web scraping. Web scraping is a very common problem, and there are tools in almost every programming language to scrape the Web (if you are already using Python, you would probably choose Beautiful Soup or Scrapy). Steps 2 and 3 involve solving a machine learning problem and require the use of sophisticated mathematical languages or frameworks, such as Weka or MATLAB, which are only a few of the vast variety of tools that provide machine learning functionality. Similarly, step 4 can be implemented in many ways using many different tools. There isn't one right answer. Since this is a problem that has been amply studied and solved (to a reasonable extent) by a lot of scientists and software developers, getting a working solution would not be difficult. However, there are issues, such as stability and scalability, that might severely restrict your choice of programming languages, web frameworks, or machine learning algorithms in each step of the problem. This is where Python wins over most other programming languages. All the preceding steps (and more) can be accomplished with only Python and a few third-party Python libraries. This flexibility and ease of developing software in Python is precisely what makes it a comfortable host for a scientific computing ecosystem. A very interesting interpretation of Python's prowess as a mature application development language can be found in Python Data Analysis, Ivan Idris, Packt Publishing. Precisely, Python is a language that is used for rapid prototyping, and it is also used to build production-quality software because of the vast scientific ecosystem it has acquired over time. The cornerstone of this ecosystem is NumPy.

Numerical Python (NumPy) is a successor to the Numeric package. It was originally written by Travis Oliphant to be the foundation of a scientific computing environment in Python. It branched off from the much wider SciPy module in early 2005 and had its first stable release in mid-2006. Since then, it has enjoyed growing popularity among Pythonists who work in the mathematics, science, and engineering fields. The goal of this book is to make you conversant enough with NumPy so that you're able to use it and can build complex scientific applications with it.