Numpy is a Python package (library) for processing multi-dimensional arrays. To set terminology, consider a spreadsheet in Excel or some similar package. A data value in a single cell would be a scalar, or a single-dimension array of length 1. A column of 10 numbers would be a single-dimensional array of length 10. A sheet of 10 columns by 15 rows would be a two-dimensional array of shape (10, 15). If we then use 5 separate worksheets with each one containing a 10 by 15 worksheet, we have a three-dimensional array of shape (10, 15, 5). Numpy is designed to create, manipulate, and efficiently compute on these arrays.
Let's begin a tour of Numpy by importing it and creating a small array, and then finding its size and shape, and computing some built-in Numpy functions on it. Note that we can easily manipulate the shape of an array.
There are numerous ways to create arrays, including some built-in functions for frequently used arrays containing 0's or 1's, or loading data from a file or even a database (with a bit more work).
Notice that you can perform mathematical operations on arrays, and that the default is to compute results elementwise, or element by element, like you would do in a spreadsheet. You can also use matrix computations as in linear algebra, with a slightly different syntax. A matrix multiplication of arrays A and B would be: dot(A,B). In the examples below, note the use of assignment to a variable (using an =), as compared to the use of an assertion that two arrays are equal (using ==). Also note the use of a where statement, which allows assignment of different values where the statement is evaluated as truo or false. These examples cover some of the more commonly used manipulations of arrays in OPUS and UrbanSim.
Numpy provides sophisticated numerical capabilities for doing statistical or econometric modeling, with a very consice syntax. An example of a tutorial script to estimate the parameters of a multiple linear regression, using Ordinary Least Squares estimation, is shown in the Appendix to this chapter. The script is from http://www.scipy.org/Cookbook/OLS.
Numpy contains a very large number of built-in functions. The following, for example, are some built-in random number functions:
For a more complete tutorial on Numpy, please refer to the one at http://www.scipy.org/Tentative_NumPy_Tutorial.