Multi-Dimensional Array (ndarray)

class cupy.ndarray

Multi-dimensional array on a CUDA device.

This class implements a subset of methods of numpy.ndarray. The difference is that this class allocates the array content on the current GPU device.

Parameters:
  • shape (tuple of ints) – Length of axes.
  • dtype – Data type. It must be an argument of numpy.dtype.
  • memptr (cupy.cuda.MemoryPointer) – Pointer to the array content head.
  • strides (tuple of ints) – The strides for axes.
  • order ({'C', 'F'}) – Row-major (C-style) or column-major (Fortran-style) order.
Variables:
T

Shape-reversed view of the array.

If ndim < 2, then this is just a reference to the array itself.

__abs__
__add__

x.__add__(y) <==> x+y

__and__

x.__and__(y) <==> x&y

__delitem__

x.__delitem__(y) <==> del x[y]

__div__

x.__div__(y) <==> x/y

__divmod__
__eq__

x.__eq__(y) <==> x==y

__float__
__floordiv__

x.__floordiv__(y) <==> x//y

__ge__

x.__ge__(y) <==> x>=y

__getitem__

x.__getitem__(y) <==> x[y]

Supports both basic and advanced indexing.

Note

Currently, it does not support slices that consists of more than one boolean arrays

Note

CuPy handles out-of-bounds indices differently from NumPy. NumPy handles them by raising an error, but CuPy wraps around them.

>>> a = cupy.arange(3)
>>> a[[1, 3]]
array([1, 0])
__gt__

x.__gt__(y) <==> x>y

__hex__
__iadd__

x.__iadd__(y) <==> x+=y

__iand__

x.__iand__(y) <==> x&=y

__idiv__

x.__idiv__(y) <==> x/=y

__ifloordiv__

x.__ifloordiv__(y) <==> x//=y

__ilshift__

x.__ilshift__(y) <==> x<<=y

__imod__

x.__imod__(y) <==> x%=y

__imul__

x.__imul__(y) <==> x*=y

__int__
__invert__

x.__invert__() <==> ~x

__ior__

x.__ior__(y) <==> x|=y

__ipow__

x.__ipow__(y) <==> x**=y

__irshift__

x.__irshift__(y) <==> x>>=y

__isub__

x.__isub__(y) <==> x-=y

__itruediv__

x.__itruediv__(y) <==> x/=y

__ixor__

x.__ixor__(y) <==> x^=y

__le__

x.__le__(y) <==> x<=y

__len__
__long__
__lshift__

x.__lshift__(y) <==> x<<y

__lt__

x.__lt__(y) <==> x<y

__mod__

x.__mod__(y) <==> x%y

__mul__

x.__mul__(y) <==> x*y

__ne__

x.__ne__(y) <==> x!=y

__neg__

x.__neg__() <==> -x

__nonzero__

x.__nonzero__() <==> x != 0

__oct__
__or__

x.__or__(y) <==> x|y

__pos__

x.__pos__() <==> +x

__pow__
__radd__

x.__radd__(y) <==> y+x

__rand__

x.__rand__(y) <==> y&x

__rdiv__

x.__rdiv__(y) <==> y/x

__rdivmod__
__repr__
__rfloordiv__

x.__rfloordiv__(y) <==> y//x

__rlshift__

x.__rlshift__(y) <==> y<<x

__rmod__

x.__rmod__(y) <==> y%x

__rmul__

x.__rmul__(y) <==> y*x

__ror__

x.__ror__(y) <==> y|x

__rpow__
__rrshift__

x.__rrshift__(y) <==> y>>x

__rshift__

x.__rshift__(y) <==> x>>y

__rsub__

x.__rsub__(y) <==> y-x

__rtruediv__

x.__rtruediv__(y) <==> y/x

__rxor__

x.__rxor__(y) <==> y^x

__setitem__

x.__setitem__(slices, y) <==> x[slices] = y

Supports both basic and advanced indexing.

Note

Currently, it does not support slices that consists of more than one boolean arrays

Note

CuPy handles out-of-bounds indices differently from NumPy when using integer array indexing. NumPy handles them by raising an error, but CuPy wraps around them.

>>> import cupy
>>> x = cupy.arange(3)
>>> x[[1, 3]] = 10
>>> x
array([10, 10,  2])

Note

The behavior differs from NumPy when integer arrays in slices reference the same location multiple times. In that case, the value that is actually stored is undefined.

>>> import cupy; import numpy
>>> a = cupy.zeros((2,))
>>> i = cupy.arange(10000) % 2
>>> v = cupy.arange(10000).astype(numpy.float)
>>> a[i] = v
>>> a  
array([ 9150.,  9151.])

On the other hand, NumPy stores the value corresponding to the last index among the indices referencing duplicate locations.

>>> import numpy
>>> a_cpu = numpy.zeros((2,))
>>> i_cpu = numpy.arange(10000) % 2
>>> v_cpu = numpy.arange(10000).astype(numpy.float)
>>> a_cpu[i_cpu] = v_cpu
>>> a_cpu
array([ 9998.,  9999.])
__str__
__sub__

x.__sub__(y) <==> x-y

__truediv__

x.__truediv__(y) <==> x/y

__xor__

x.__xor__(y) <==> x^y

argmax()

Returns the indices of the maximum along a given axis.

See also

cupy.argmax() for full documentation, numpy.ndarray.argmax()

argmin()

Returns the indices of the minimum along a given axis.

See also

cupy.argmin() for full documentation, numpy.ndarray.argmin()

astype()

Casts the array to given data type.

Parameters:
  • dtype – Type specifier.
  • copy (bool) – If it is False and no cast happens, then this method returns the array itself. Otherwise, a copy is returned.
Returns:

If copy is False and no cast is required, then the array itself is returned. Otherwise, it returns a (possibly casted) copy of the array.

Note

This method currently does not support order, casting, and subok arguments.

clip()

Returns an array with values limited to [a_min, a_max].

See also

cupy.clip() for full documentation, numpy.ndarray.clip()

copy()

Returns a copy of the array.

Parameters:order ({'C', 'F'}) – Row-major (C-style) or column-major (Fortran-style) order. This function currently does not support order ‘A’ and ‘K’.

See also

cupy.copy() for full documentation, numpy.ndarray.copy()

cstruct

C representation of the array.

This property is used for sending an array to CUDA kernels. The type of returned C structure is different for different dtypes and ndims. The definition of C type is written in cupy/carray.cuh.

device

CUDA device on which this array resides.

diagonal()

Returns a view of the specified diagonals.

See also

cupy.diagonal() for full documentation, numpy.ndarray.diagonal()

dot()

Returns the dot product with given array.

See also

cupy.dot() for full documentation, numpy.ndarray.dot()

dump()

Dumps a pickle of the array to a file.

Dumped file can be read back to cupy.ndarray by cupy.load().

dumps()

Dumps a pickle of the array to a string.

fill()

Fills the array with a scalar value.

Parameters:value – A scalar value to fill the array content.
flags

Object containing memory-layout information.

It only contains c_contiguous, f_contiguous, and owndata attributes. All of these are read-only. Accessing by indexes is also supported.

flatten()

Returns a copy of the array flatten into one dimension.

It currently supports C-order only.

Returns:A copy of the array with one dimension.
Return type:cupy.ndarray
get()

Returns a copy of the array on host memory.

Parameters:stream (cupy.cuda.Stream) – CUDA stream object. If it is given, the copy runs asynchronously. Otherwise, the copy is synchronous.
Returns:Copy of the array on host memory.
Return type:numpy.ndarray
itemsize

Size of each element in bytes.

max()

Returns the maximum along a given axis.

See also

cupy.amax() for full documentation, numpy.ndarray.max()

mean()

Returns the mean along a given axis.

See also

cupy.mean() for full documentation, numpy.ndarray.mean()

min()

Returns the minimum along a given axis.

See also

cupy.amin() for full documentation, numpy.ndarray.min()

nbytes

Size of whole elements in bytes.

It does not count skips between elements.

ndim

Number of dimensions.

a.ndim is equivalent to len(a.shape).

nonzero()

Return the indices of the elements that are non-zero.

Returned Array is containing the indices of the non-zero elements in that dimension.

Returns:Indices of elements that are non-zero.
Return type:tuple of arrays

See also

numpy.nonzero()

prod()

Returns the product along a given axis.

See also

cupy.prod() for full documentation, numpy.ndarray.prod()

ravel()

Returns an array flattened into one dimension.

See also

cupy.ravel() for full documentation, numpy.ndarray.ravel()

reduced_view()

Returns a view of the array with minimum number of dimensions.

Parameters:dtype – Data type specifier. If it is given, then the memory sequence is reinterpreted as the new type.
Returns:A view of the array with reduced dimensions.
Return type:cupy.ndarray
repeat()

Returns an array with repeated arrays along an axis.

See also

cupy.repeat() for full documentation, numpy.ndarray.repeat()

reshape()

Returns an array of a different shape and the same content.

See also

cupy.reshape() for full documentation, numpy.ndarray.reshape()

scatter_add()

Adds given values to specified elements of an array.

See also

cupy.scatter_add() for full documentation.

set()

Copies an array on the host memory to cupy.ndarray.

Parameters:
  • arr (numpy.ndarray) – The source array on the host memory.
  • stream (cupy.cuda.Stream) – CUDA stream object. If it is given, the copy runs asynchronously. Otherwise, the copy is synchronous.
shape

Lengths of axes.

Setter of this property involves reshaping without copy. If the array cannot be reshaped without copy, it raises an exception.

squeeze()

Returns a view with size-one axes removed.

See also

cupy.squeeze() for full documentation, numpy.ndarray.squeeze()

std()

Returns the standard deviation along a given axis.

See also

cupy.std() for full documentation, numpy.ndarray.std()

strides

Strides of axes in bytes.

sum()

Returns the sum along a given axis.

See also

cupy.sum() for full documentation, numpy.ndarray.sum()

swapaxes()

Returns a view of the array with two axes swapped.

See also

cupy.swapaxes() for full documentation, numpy.ndarray.swapaxes()

take()

Returns an array of elements at given indices along the axis.

See also

cupy.take() for full documentation, numpy.ndarray.take()

tofile()

Writes the array to a file.

tolist()

Converts the array to a (possibly nested) Python list.

Returns:The possibly nested Python list of array elements.
Return type:list
trace()

Returns the sum along diagonals of the array.

See also

cupy.trace() for full documentation, numpy.ndarray.trace()

transpose()

Returns a view of the array with axes permuted.

See also

cupy.transpose() for full documentation, numpy.ndarray.reshape()

var()

Returns the variance along a given axis.

See also

cupy.var() for full documentation, numpy.ndarray.var()

view()

Returns a view of the array.

Parameters:dtype – If this is different from the data type of the array, the returned view reinterpret the memory sequence as an array of this type.
Returns:A view of the array. A reference to the original array is stored at the base attribute.
Return type:cupy.ndarray
cupy.asnumpy(a, stream=None)[source]

Returns an array on the host memory from an arbitrary source array.

Parameters:
  • a – Arbitrary object that can be converted to numpy.ndarray.
  • stream (cupy.cuda.Stream) – CUDA stream object. If it is specified, then the device-to-host copy runs asynchronously. Otherwise, the copy is synchronous. Note that if a is not a cupy.ndarray object, then this argument has no effect.
Returns:

Converted array on the host memory.

Return type:

numpy.ndarray