numpy.ndarray

class numpy.ndarray

An array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array (its byte-order, how many bytes it occupies in memory, whether it is an integer or a floating point number, etc.).

Arrays should be constructed using array, zeros or empty (refer to the See Also section below). The parameters given here describe a low-level method for instantiating an array (ndarray(...)).

For more information, refer to the numpy module and examine the the methods and attributes of an array.

Parameters:

shape : tuple of ints

Shape of created array.

dtype : data type, optional

Any object that can be interpreted a numpy data type.

buffer : object exposing buffer interface, optional

Used to fill the array with data.

offset : int, optional

Offset of array data in buffer.

strides : tuple of ints, optional

Strides of data in memory.

order : {‘C’, ‘F’}, optional

Row-major or column-major order.

Attributes:

T : ndarray

Transponent of the array.

data : buffer

Array data in memory.

dtype : data type

Data type, describing the format of the elements in the array.

flags : dict

Dictionary containing information related to memory use, e.g., ‘C_CONTIGUOUS’, ‘OWNDATA’, ‘WRITEABLE’, and others.

flat : ndarray

Return flattened version of the array as an iterator. The iterator allows assignments, e.g., x.flat = 3.

imag : ndarray

Imaginary part of the array.

real : ndarray

Real part of the array.

size : int

Number of elements in the array.

itemsize : int

The size of each element in memory (in bytes).

nbytes : int

The total number of bytes required to store the array data, i.e., itemsize * size.

shape : tuple of ints

Shape of the array.

strides : tuple of ints

The step-size required to move from one element to the next in memory. For example, a contiguous (3, 4) array of type int16 in C-order has strides (8, 2). This implies that to move from element to element in memory requires jumps of 2 bytes. To move from row-to-row, one needs to jump 6 bytes at a time (2 * 4).

ctypes : ctypes object

Class containing properties of the array needed for interaction with ctypes.

base : ndarray

If the array is a view on another array, that array is its base (unless that array is also a view). The base array is where the array data is ultimately stored.

See also

array
Construct an array.
zeros
Create an array and fill its allocated memory with zeros.
empty
Create an array, but leave its allocated memory unchanged.
dtype
Create a data type.

Notes

There are two modes of creating an array using __new__:

  1. If buffer is None, then only shape, dtype, and order are used.
  2. If buffer is an object exporting the buffer interface, then all keywords are interpreted.

No __init__ method is needed because the array is fully initialized after the __new__ method.

Examples

These examples illustrate the low-level ndarray constructor. Refer to the See Also section for easier ways of constructing an ndarray.

First mode, buffer is None:

>>> np.ndarray(shape=(2,2), dtype=float, order='F')
array([[ -1.13698227e+002,   4.25087011e-303],
       [  2.88528414e-306,   3.27025015e-309]])

Second mode:

>>> np.ndarray((2,), buffer=np.array([1,2,3]),
...            offset=np.int_().itemsize,
...            dtype=int) # offset = 1*itemsize, i.e. skip first element
array([2, 3])

Previous topic

The N-dimensional array (ndarray)

Next topic

numpy.ndarray.flags

This Page

Quick search