6 - NumPy
Published online by Cambridge University Press: 05 February 2016
Summary
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.
- Type
- Chapter
- Information
- Learning Scientific Programming with Python , pp. 184 - 279Publisher: Cambridge University PressPrint publication year: 2016