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, 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 the methods and attributes of an array.

Parameters :

(for the __new__ method; see Notes below) :

shape : tuple of ints

Shape of created array.

dtype : data-type, optional

Any object that can be interpreted as 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.

See also

array
Construct an array.
zeros
Create an array, each element of which is zero.
empty
Create an array, but leave its allocated memory unchanged (i.e., it contains “garbage”).
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 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:

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

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
data
dtype Create a data type object.
flags
flat
imag(val) Return the imaginary part of the elements of the array.
real(val) Return the real part of the elements of the array.
size(a[, axis]) Return the number of elements along a given axis.
itemsize
nbytes Base object for a dictionary for look-up with any alias for an array dtype.
ndim(a) Return the number of dimensions of an array.
shape(a) Return the shape of an array.
strides
ctypes create and manipulate C data types in Python
base

Methods

all(a[, axis, out]) Test whether all array elements along a given axis evaluate to True.
any(a[, axis, out]) Test whether any array element along a given axis evaluates to True.
argmax(a[, axis]) Indices of the maximum values along an axis.
argmin(a[, axis]) Return the indices of the minimum values along an axis.
argsort(a[, axis, kind, order]) Returns the indices that would sort an array.
astype
byteswap
choose(a, choices[, out, mode]) Construct an array from an index array and a set of arrays to choose from.
clip(a, a_min, a_max[, out]) Clip (limit) the values in an array.
compress(condition, a[, axis, out]) Return selected slices of an array along given axis.
conj() Return the complex conjugate, element-wise.
conjugate() Return the complex conjugate, element-wise.
copy(a) Return an array copy of the given object.
cumprod(a[, axis, dtype, out]) Return the cumulative product of elements along a given axis.
cumsum(a[, axis, dtype, out]) Return the cumulative sum of the elements along a given axis.
diagonal(a[, offset, axis1, axis2]) Return specified diagonals.
dot(a, b) Dot product of two arrays.
dump
dumps
fill
flatten
getfield
item
itemset
max(a[, axis, out]) Return the maximum of an array or maximum along an axis.
mean(a[, axis, dtype, out]) Compute the arithmetic mean along the specified axis.
min(a[, axis, out]) Return the minimum of an array or minimum along an axis.
newbyteorder
nonzero(a) Return the indices of the elements that are non-zero.
prod(a[, axis, dtype, out]) Return the product of array elements over a given axis.
ptp(a[, axis, out]) Range of values (maximum - minimum) along an axis.
put(a, ind, v[, mode]) Replaces specified elements of an array with given values.
ravel(a[, order]) Return a flattened array.
repeat(a, repeats[, axis]) Repeat elements of an array.
reshape(a, newshape[, order]) Gives a new shape to an array without changing its data.
resize(a, new_shape) Return a new array with the specified shape.
round(a[, decimals, out]) Round an array to the given number of decimals.
searchsorted(a, v[, side]) Find indices where elements should be inserted to maintain order.
setfield
setflags
sort(a[, axis, kind, order]) Return a sorted copy of an array.
squeeze(a) Remove single-dimensional entries from the shape of an array.
std(a[, axis, dtype, out, ddof]) Compute the standard deviation along the specified axis.
sum(a[, axis, dtype, out]) Sum of array elements over a given axis.
swapaxes(a, axis1, axis2) Interchange two axes of an array.
take(a, indices[, axis, out, mode]) Take elements from an array along an axis.
tofile
tolist
tostring
trace(a[, offset, axis1, axis2, dtype, out]) Return the sum along diagonals of the array.
transpose(a[, axes]) Permute the dimensions of an array.
var(a[, axis, dtype, out, ddof]) Compute the variance along the specified axis.
view

Previous topic

numpy.ndarray.__repr__

This Page