numpy.ndarray — NumPy v2.2 Manual (original) (raw)

class numpy.ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)[source]#

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, zeros or empty (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 numpy module and examine the methods and attributes of an array.

Parameters:

(for the __new__ method; see Notes below)

shapetuple of ints

Shape of created array.

dtypedata-type, optional

Any object that can be interpreted as a numpy data type.

bufferobject exposing buffer interface, optional

Used to fill the array with data.

offsetint, optional

Offset of array data in buffer.

stridestuple of ints, optional

Strides of data in memory.

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

Row-major (C-style) or column-major (Fortran-style) order.

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 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 ndarray constructor. 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:

Tndarray

View of the transposed array.

databuffer

Python buffer object pointing to the start of the array’s data.

dtypedtype object

Data-type of the array’s elements.

flagsdict

Information about the memory layout of the array.

flatnumpy.flatiter object

A 1-D iterator over the array.

imagndarray

The imaginary part of the array.

realndarray

The real part of the array.

sizeint

Number of elements in the array.

itemsizeint

Length of one array element in bytes.

nbytesint

Total bytes consumed by the elements of the array.

ndimint

Number of array dimensions.

shapetuple of ints

Tuple of array dimensions.

stridestuple of ints

Tuple of bytes to step in each dimension when traversing an array.

ctypesctypes object

An object to simplify the interaction of the array with the ctypes module.

basendarray

Base object if memory is from some other object.

Methods