Published online by Cambridge University Press: 05 February 2016
NumPy has become the de facto standard package for general scientific programming in Python. Its core object is the ndarray, a multidimensional array of a single data type which can be sorted, reshaped, subject to mathematical operations and statistical analysis, written to and read from files, and much more. The NumPy implementations of these mathematical operations and algorithms have two main advantages over the “core” Python objects we have used until now. First, they are implemented as precompiled C code and so approach the speed of execution of a program written in C itself; second, NumPy supports vectorization: a single operation can be carried out on an entire array, rather than requiring an explicit loop over the array's elements. For example, compare the multiplication of two one-dimensional lists of n numbers, a and b, in the core python language:
c = []
for i in range(n):
c.append(a[i] * b[i])
and using NumPy arrays:
c = a * b
The elementwise multiplication is handled by optimized, precompiled C and so is very fast (much faster for large n than the core Python alternative). The absence of explicit looping and indexing makes the code cleaner, less error-prone and closer to the standard mathematical notation it reflects.
All of NumPy's functionality is provided by the numpy package. To use it, it is strongly advised to import with
import numpy as np
and then to refer to its attributes with the prefix np. (e.g., np.array). This is the way we use NumPy in this book.
Basic array methods
The NumPy array class is ndarray, which consists of a multidimensional table of elements indexed by a tuple of integers. Unlike Python lists and tuples, the elements cannot be of different types: each element in a NumPy array has the same type, which is specified by an associated data type object (dtype). The dtype of an array specifies not only the broad class of element (integer, floating point number, etc.) but also how it is represented in memory (e.g., how many bits it occupies) – see Section 6.1.2.
The dimensions of a NumPy array are called axes; the number of axes an array has is called its rank.
To save this book to your Kindle, first ensure no-reply@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.