The N-dimensional array (ndarray)
An is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its , which is a tuple of N positive integers that specify the sizes of each dimension. The type of items in the array is specified by a separate , one of which is associated with each ndarray.
As with other container objects in Python, the contents of an can be accessed and modified by the array (using, for example, N integers), and via the methods and attributes of the .
Different can share the same data, so that changes made in one may be visible in another. That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implementing the buffer or interfaces.
Example
A 2-dimensional array of size 2 x 3, composed of 4-byte integer elements:
>>> x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)>>> type(x)>>> x.shape(2, 3)>>> x.dtypedtype('int32') The array can be indexed using Python container-like syntax:
>>> x[1,2] # i.e., the element of x in the *second* row, *third*column, namely, 6.For example can produce views of the array:
>>> y = x[:,1]>>> yarray([2, 5])>>> y[0] = 9 # this also changes the corresponding element in x>>> yarray([9, 5])>>> xarray([[1, 9, 3], [4, 5, 6]])