NumPy
arrays are objects of the ndarray
class, which represents a fixed-size multidimensional collection of homogeneous data.
Here, we will assume that the NumPy
library has been imported using the following command line:
import numpy as np
Once we have done that, we can create ndarray
(from now on, informally called array object or simply array) from a list of lists as indicated in the following command line:
a = np.array([[-2,3,-4,0],[2,-7,0,0],[3,-4,2,1]],dtype=np.float64) print a
Contrary to Python lists and tuples, all entries of an array object must be of the same type. The types themselves are represented by NumPy
objects and are referred to as dtype
(from data type) of the array. In the preceding example, we explicitly specify dtype
as float64
, which represents a 64-bit floating-point value.
Arrays have several attributes that give information about the data layout. The more commonly used ones are as follows:
a.shape
The preceding command returns the tuple (3, 4)
, since this is a two-dimensional array with three rows and four columns. Somewhat surprisingly, the shape attribute is not read-only and we can use it to reshape the array:
a.shape = (6,2) print a
After running the preceding example, run a.shape(3,4)
to return to the original dimensions.
a.ndim
This, of course, returns 2
. An important notion in NumPy
is the idea of axes of an array. A two dimensional array has two axes, numbered 0 and 1. If we think of the array as representing a mathematical matrix, axis 0 is vertical and points down, and axis 1 is horizontal and points to the right. Certain array methods have an optional axis
keyword argument that lets the user specify along which axis the operation is performed.
a.size
In the preceding example, the output returned is 12
, as expected.
b = a.T print b
An important thing that this creates is a view of the array a
. The NumPy
package is designed to work efficiently with very large arrays, and in most cases, avoids making copies of data unless absolutely necessary, or is explicitly directed to do so.
print a b[1,2] = 11 print a
Note that the entry 2, 1
of the array a
is changed, demonstrating that both variables, a
and b
, point to the same area in memory.
empty()
function as follows:c = np.empty(shape=(3,2), dtype=np.float64) print c
zeros()
or ones()
function as follows:There are also functions that create new arrays with the same shape and data type of an existing array:
ones_like()
and empty_like()
produce arrays of ones and uninitialized data with the same shape as a given array.NumPy
also has the eye()
function that returns an identity array of the given dimension and dtype
:f = np.eye(5, dtype=np.float64) print f
The number of rows and columns do not have to be the same. In this case, the resulting matrix will only be a left- or right- identity, as applicable:
g = np.eye(5, 3, dtype=np.float64) print g
copy()
function clones an array as follows:aa = np.copy(a) print a print aa
frombuffer()
function creates an array from an object that exposes the (one-dimensional) buffer interface. Here is an example:ar = np.arange(0.0, 1.0, 0.1, dtype=np.float64) v = np.frombuffer(ar) v.shape = (2, 5) print v
The arange()
function is a NumPy
extension of the Python range. It has a similar syntax, but allows ranges of floating-point values.
loadtxt()
function reads an array from a text file. Suppose the text file matrix.txt
contains the following data:1.3 4.6 7.8 -3.6 0.4 3.54 2.4 1.7 4.5
Then, we can read the data with the following command:
h = np.loadtxt('matrix.txt', dtype=np.float64) print h
.npy
format:np.save('matrix.npy',h) hh = np.load('matrix.npy') print hh