Multi-Dimensional Array (ndarray)
dpnp.ndarray
is the DPNP counterpart of NumPy numpy.ndarray
.
For the basic concept of ndarray
s, please refer to the NumPy documentation.
alias of |
|
An array object represents a multidimensional tensor of numeric elements stored in a USM allocation on a SYCL device. |
Constructing arrays
New arrays can be constructed using the routines detailed in
Array Creation Routines, and also by using the low-level
dpnp.ndarray
constructor:
alias of |
Indexing arrays
Arrays can be indexed using an extended Python slicing syntax,
array[selection]
.
See also
Array attributes
Array attributes reflect information that is intrinsic to the array itself. Generally, accessing an array through its attributes allows you to get and sometimes set intrinsic properties of the array without creating a new array. The exposed attributes are the core parts of an array and only some of them can be reset meaningfully without creating a new array. Information on each attribute is given below.
Memory layout
The following attributes contain information about the memory layout of the array:
Return information about the memory layout of the array. |
|
Tuple of array dimensions. |
|
Return memory displacement in array elements, upon unit change of respective index. |
|
Return the number of dimensions of an array. |
|
Python object pointing to the start of USM memory allocation with the array's data. |
|
Number of elements in the array. |
|
Size of one array element in bytes. |
|
Total bytes consumed by the elements of the array. |
|
Return |
|
Return |
|
Return |
|
Return |
|
USM type of underlying memory. |
Data type
See also
The data type object associated with the array can be found in the
dtype
attribute:
Return NumPy's dtype corresponding to the type of the array elements. |
Other attributes
View of the transposed array. |
|
View of the matrix transposed array. |
|
The real part of the array. |
|
The imaginary part of the array. |
|
Return a flat iterator, or set a flattened version of self to value. |
Special attributes
Give |
|
Property to support |
Array methods
An dpnp.ndarray
object has many methods which operate on or with
the array in some fashion, typically returning an array result. These
methods are briefly explained below. (Each method's docstring has a
more complete description.)
For the following methods there are also corresponding functions in
dpnp
: all
, any
,
argmax
, argmin
,
argpartition
, argsort
,
choose
, clip
,
compress
, copy
,
cumprod
, cumsum
,
diagonal
, imag
,
max
, mean
, min
,
nonzero
, partition
,
prod
, put
,
ravel
, real
, repeat
,
reshape
, round
,
searchsorted
, sort
,
squeeze
, std
, sum
,
swapaxes
, take
, trace
,
transpose
, var
.
Array conversion
Copy an element of an array to a standard Python scalar and return it. |
|
Copy the array with data type casting. |
|
Return a copy of the array. |
|
New view of array with the same data. |
|
Fill the array with a scalar value. |
|
Get |
Shape manipulation
For reshape, resize, and transpose, the single tuple argument may be
replaced with n
integers which will be interpreted as an n-tuple.
Return an array containing the same data with a new shape. |
|
Return a view of the array with axes transposed. |
|
Interchange two axes of an array. |
|
Return a copy of the array collapsed into one dimension. |
|
Return a contiguous flattened array. |
|
Remove single-dimensional entries from the shape of an array. |
Item selection and manipulation
For array methods that take an axis keyword, it defaults to None. If axis is None, then the array is treated as a 1-D array. Any other value for axis represents the dimension along which the operation should proceed.
Take elements from an array along an axis. |
|
Put values of an array into another array along a given axis. |
|
Repeat elements of an array. |
|
Use an array as index array to construct a new array from a set of choices. |
|
Sort an array in-place. |
|
Return an ndarray of indices that sort the array along the specified axis. |
|
Return a partitioned copy of an array. |
|
Find indices where elements of v should be inserted in a to maintain order. |
|
Return the indices of the elements that are non-zero. |
|
Select slices of an array along a given axis. |
|
Return specified diagonals. |
Calculation
Many of these methods take an argument named axis. In such cases,
If axis is None (the default), the array is treated as a 1-D array and the operation is performed over the entire array. This behavior is also the default if self is a 0-dimensional array.
If axis is an integer, then the operation is done over the given axis (for each 1-D subarray that can be created along the given axis).
The parameter dtype specifies the data type over which a reduction operation (like summing) should take place. The default reduce data type is the same as the data type of self. To avoid overflow, it can be useful to perform the reduction using a larger data type.
For several methods, an optional out argument can also be provided and the
result will be placed into the output array given. The out argument must be
an dpnp.ndarray
and have the same number of elements as the result
array. It can have a different data type in which case casting will be
performed.
Return the maximum along an axis. |
|
Return array of indices of the maximum values along the given axis. |
|
Return the minimum along a given axis. |
|
Return array of indices to the minimum values along the given axis. |
|
Clip (limit) the values in an array. |
|
Complex-conjugate all elements. |
|
Return the complex conjugate, element-wise. |
|
Return array with each element rounded to the given number of decimals. |
|
Return the sum along diagonals of the array. |
|
Return the sum along a given axis. |
|
Return the cumulative sum of the elements along the given axis. |
|
Return the average of the array elements. |
|
Return the variance of the array elements, along given axis. |
|
Return the standard deviation of the array elements, along given axis. |
|
Return the prod along a given axis. |
|
Return the cumulative product of the elements along the given axis. |
|
Return |
|
Return |
Arithmetic, matrix multiplication, and comparison operations
Arithmetic and comparison operations on dpnp.ndarrays
are defined as element-wise operations, and generally yield
dpnp.ndarray
objects as results.
Each of the arithmetic operations (+
, -
, *
, /
, //
, %
,
divmod()
, **
or pow()
, <<
, >>
, &
, ^
, |
, ~
)
and the comparisons (==
, <
, >
, <=
, >=
, !=
) is
equivalent to the corresponding universal function (or ufunc for short)
in DPNP. For more information, see the section on Universal Functions.
Comparison operators:
Return \(\text{self < value}\). |
|
Return \(\text{self <= value}\). |
|
Return \(\text{self > value}\). |
|
Return \(\text{self >= value}\). |
|
Return \(\text{self == value}\). |
|
Return \(\text{self != value}\). |
Truth value of an array (bool()
):
|
Note
Truth-value testing of an array invokes
dpnp.ndarray.__bool__()
, which raises an error if the number of
elements in the array is not 1, because the truth value
of such arrays is ambiguous. Use .any()
and
.all()
instead to be clear about what is meant
in such cases. (If you wish to check for whether an array is empty,
use for example .size > 0
.)
Unary operations:
Return \(\text{-self}\). |
|
Return \(\text{+self}\). |
|
Return \(|\text{self}|\). |
|
Return \(\text{~self}\). |
Arithmetic:
Return \(\text{self + value}\). |
|
Return \(\text{self - value}\). |
|
Return \(\text{self * value}\). |
|
Return \(\text{self / value}\). |
|
Return \(\text{self // value}\). |
|
Return \(\text{self % value}\). |
|
Return \(\text{self ** value}\). |
|
Return \(\text{self << value}\). |
|
Return \(\text{self >> value}\). |
|
Return \(\text{self & value}\). |
|
Return \(\text{self | value}\). |
|
Return \(\text{self ^ value}\). |
Arithmetic, reflected:
Return \(\text{value + self}\). |
|
Return \(\text{value - self}\). |
|
Return \(\text{value * self}\). |
|
Return \(\text{value / self}\). |
|
Return \(\text{value // self}\). |
|
Return \(\text{value % self}\). |
|
Return \(\text{value ** self}\). |
|
Return \(\text{value << self}\). |
|
Return \(\text{value >> self}\). |
|
Return \(\text{value & self}\). |
|
Return \(\text{value | self}\). |
|
Return \(\text{value ^ self}\). |
Arithmetic, in-place:
Return \(\text{self += value}\). |
|
Return \(\text{self -= value}\). |
|
Return \(\text{self *= value}\). |
|
Return \(\text{self /= value}\). |
|
Return \(\text{self //= value}\). |
|
Return \(\text{self %= value}\). |
|
Return \(\text{self **= value}\). |
|
Return \(\text{self <<= value}\). |
|
Return \(\text{self >>= value}\). |
|
Return \(\text{self &= value}\). |
|
Return \(\text{self |= value}\). |
|
Return \(\text{self ^= value}\). |
Matrix Multiplication:
Return \(\text{self @ value}\). |
|
Return \(\text{value @ self}\). |
|
Return \(\text{self @= value}\). |
Special methods
For standard library functions:
Used if |
Basic customization:
NumPy's array protocol method to disallow implicit conversion. |
|
Return array namespace, member functions of which implement data API. |
|
Produce DLPack capsule. |
|
Give a tuple ( |
Container customization: (see Indexing)
Return \(\text{len(self)}\). |
|
Return \(\text{iter(self)}\). |
|
Return \(\text{self[key]}\). |
|
Set \(\text{self[key]}\) to a value. |
|
Return \(\text{value in self}\). |
Conversion; the operations int()
, float()
,
complex()
and operator.index()
.
They work only on arrays that have one element in them
and return the appropriate scalar.
Convert a zero-dimensional array to a Python int object. |
|
Convert a zero-dimensional array to a Python int object. |
|
Convert a zero-dimensional array to a Python float object. |
|
Convert a zero-dimensional array to a Python complex object. |
String representations:
Return \(\text{str(self)}\). |
|
Return \(\text{repr(self)}\). |