Primary
numpy.ndarray() ⚬|Documentation|1st|20251021001533-00-⌔
numpy.ndarray — NumPy v2.1 Manual#numpy.ndarray
class
numpy.ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)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, a floating point number, or something else, etc.)
Arrays should be constructed using
array,zerosorempty(refer to the See Also section below). The parameters given here refer to a low-level method (ndarray(…)) for instantiating an array.For more information, refer to the
numpymodule and examine the methods and attributes of an array.Parameters:
(for the new method; see Notes below)
shape: tuple of intsShape of created array.
dtype: data-type, optionalAny object that can be interpreted as a numpy data type.
buffer: object exposing buffer interface, optionalUsed to fill the array with data.
offset: int, optionalOffset of array data in buffer.
strides: tuple of ints, optionalStrides of data in memory.
order: {‘C’, ‘F’}, optionalRow-major (C-style) or column-major (Fortran-style) order.
See also:
arrayConstruct an array.
zerosCreate an array, each element of which is zero.
emptyCreate an array, but leave its allocated memory unchanged (i.e., it contains “garbage”).
dtypeCreate a data-type.
numpy.typing.NDArrayAn ndarray alias generic w.r.t. its
dtype.type.Notes:
There are two modes of creating an array using
__new__:
- If buffer is None, then only
shape,dtype, and order are used.- If buffer is an object exposing 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
ndarrayconstructor. Refer to the See Also section above for easier ways of constructing an ndarray.First mode, buffer is None:
>>> import numpy as np >>> np.ndarray(shape=(2,2), dtype=float, order='F') array([[0.0e+000, 0.0e+000], # random [ nan, 2.5e-323]])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])Attributes:
T: ndarrayView of the transposed array.
data: bufferPython buffer object pointing to the start of the array’s data.
dtype: dtype objectData-type of the array’s elements.
flags: dictInformation about the memory layout of the array.
flat: numpy.flatiter objectA 1-D iterator over the array.
imag: ndarrayThe imaginary part of the array.
real: ndarrayThe real part of the array.
size: intNumber of elements in the array.
itemsize: intLength of one array element in bytes.
nbytes: intTotal bytes consumed by the elements of the array.
ndim: intNumber of array dimensions.
shape: tuple of intsTuple of array dimensions.
strides: tuple of intsTuple of bytes to step in each dimension when traversing an array.
ctypes: ctypes objectAn object to simplify the interaction of the array with the ctypes module.
base: ndarrayBase object if memory is from some other object.
Methods:
Printed 2026-06-28.
Link to original
Secondary
• • •