nlcpy.ndarray
- class nlcpy.ndarray(shape, dtype=float, strides=None, order='C', veo_hmem=None)
N-dimensional array class for VE.
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 (refer to the See Also section below). The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.
- Parameters
- shapetuple of ints
Shape of created array.
- dtypedtype, optional
Any object that can be interpreted as a numpy or nlcpy data type.
- stridestuple of ints
Strides of data in memory.
- order{'C','F'}, optional
Row-major (C-style) or column-major (Fortran-style) order.
- veo_hmemint, optional
VEO heterogeneous memory.
参考
Examples
This example illustrate the low-level ndarray constructor. Refer to the See Also section above for easier ways of constructing an ndarray.
>>> import nlcpy as vp >>> vp.ndarray(shape=(2,2), dtype=float, order='F') array([[0., 0.], # may vary [0., 0.]])
Methods
- __getitem__(key, /)
Return self[key].
- __setitem__(key, value, /)
Set self[key] to value.
- __len__(/)
Return len(self).
- __iter__(/)
Implement iter(self).
- all(self, axis=None, out=None, keepdims=False)
Returns True if all elements evaluate to True.
Refer to
nlcpy.all()
for full documentation.参考
nlcpy.all
Equivalent function.
- any(self, axis=None, out=None, keepdims=False)
Returns True if any elements evaluate to True.
Refer to
nlcpy.any()
for full documentation.参考
nlcpy.any
Equivalent function.
- argmax(self, axis=None, out=None) ndarray
Returns indices of the maximum values along the given axis.
Refer to
nlcpy.argmax()
for full documentation.参考
nlcpy.argmax
Equivalent function.
- argmin(self, axis=None, out=None) ndarray
Returns indices of the minimum values along the given axis.
Refer to
nlcpy.argmin()
for full documentation.参考
nlcpy.argmin
Equivalent function.
- argsort(self, axis=- 1, kind=None, order=None) ndarray
Returns the indices that would sort this array.
Refer to
nlcpy.argsort()
for full documentation.参考
nlcpy.argsort
Equivalent function.
- astype(self, dtype, order='K', casting=None, subok=None, copy=True) ndarray
Returns a copy of the array, casts to a specified type.
- Parameters
- dtypestr or dtype
Typecode or data-type to which the array is cast.
- order{'C','F','A','K'}, optional
Controls the memory layout order of the result. 'C' means C order, 'F' means Fortran order, 'A' means 'F' order if all the arrays are Fortran contiguous, 'C' order otherwise, and 'K' means as close to the order the array elements appear in memory as possible. Default is 'K'.
- castingstr, optional
This argument is not supported. The default is None.
- subokbool, optional
This argument is not supported. The default is None.
- copybool, optional
By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order requirements are satisfied, the input array is returned instead of a copy.
- Returns
- arr_tndarray
Unless copy is False and the other conditions for returning the input array are satisfied (see description for copy input parameter), arr_t is a new array of the same shape as the input array, with dtype, order given by dtype, order.
Examples
>>> import nlcpy as vp >>> x = vp.array([1, 2, 2.5]) >>> x array([1. , 2. , 2.5]) >>> x.astype(int) array([1, 2, 2])
- clip(self, a_min, a_max, out=None, **kwargs)
- conj(self, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=False)
Function that operates element by element on whole arrays.
参考
nlcpy.conj
Equivalent function.
- conjugate(self, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=False)
Function that operates element by element on whole arrays.
参考
nlcpy.conjugate
Equivalent function.
- copy(self, order='C') ndarray
Returns a copy of the array.
- Parameters
- order{'C','F','A','K'}, optional
Controls the memory layout of the copy. 'C' means C-order, 'F' means F-order, 'A' means 'F' if a is Fortran contiguous, 'C' otherwise. 'K' means match the layout of a as closely as possible. (Note that this function and nlcpy.copy are very similar, but have different default values for their order= arguments.)
参考
nlcpy.copy
Equivalent function.
- cumsum(self, axis=None, dtype=None, out=None)
Returns the cumulative sum of the elements along a given axis.
参考
nlcpy.cumsum
Equivalent function.
- diagonal(self, offset=0, axis1=0, axis2=1) ndarray
Returns specified diagonals.
Refer to nlcpy.diagonal for full documentation.
参考
nlcpy.diagonal
Equivalent function.
- fill(self, value)
Fills the array with a scalar value.
- Parameters
- valuescalar
All elements of the ndarray will be assigned this value.
Examples
>>> import nlcpy as vp >>> a = vp.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = vp.empty(2) >>> a.fill(1) >>> a array([1., 1.])
- flatten(self, order='C')
Returns a copy of the array collapsed into one dimension.
- Parameters
- order{'C','F','A','K'}, optional
'C' means to flatten in row-major (C-style) order. 'F' means to flatten in column-major (Fortran-style) order. 'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. 'K' means to flatten a in the order the elements occur in memory. The default is 'C'.
- Returns
- outndarray
A copy of the input array, flattened to one dimension.
参考
nlcpy.ravel
Returns a flattened array.
制限事項
If order = 'F' : NotImplementedError occurs.
- If order = 'A' or 'K' : NotImplementedError occurs when a is usingFortran-style order
Examples
>>> import nlcpy as vp >>> a = vp.array([[1,2], [3,4]]) >>> a.flatten() array([1, 2, 3, 4])
- get(self, order='C', out=None)
Returns a NumPy array on VH that copied from VE.
- Parameters
- order{'C','F','A'}, optional
Controls the memory layout order of the result. The default is 'C'. The
order
will be ignored ifout
is specified.
- max(self, axis=None, out=None, keepdims=False, initial=nlcpy._NoValue, where=True) ndarray
Returns the maximum along a given axis.
Refer to
nlcpy.amax()
for full documentation.参考
nlcpy.amax
Equivalent function.
- mean(self, axis=None, dtype=None, out=None, keepdims=nlcpy._NoValue)
Computes the arithmetic mean along the specified axis.
Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.
Refer to
nlcpy.mean()
for full documentation.参考
nlcpy.mean
Equivalent function.
- min(self, axis=None, out=None, keepdims=False, initial=nlcpy._NoValue, where=True) ndarray
Returns the minimum along a given axis.
Refer to
nlcpy.amin()
for full documentation.参考
nlcpy.amin
Equivalent function.
- nonzero(self)
Returns the indices of the elements that are non-zero.
Refer to
nlcpy.nonzero()
for full documentation.参考
nlcpy.nonzero
Equivalent function.
- prod(self, axis=None, dtype=None, out=None, keepdims=False, initial=nlcpy._NoValue, where=True)
Returns the product of the array elements over the given axis.
参考
nlcpy.prod
Equivalent function.
- ptp(self, axis=None, out=None, keepdims=nlcpy._NoValue)
Range of values (maximum - minimum) along an axis.
Refer to
nlcpy.ptp()
for full documentation.参考
nlcpy.ptp
Equivalent function.
- ravel(self, order='C')
Returns a flattened array.
Refer to
nlcpy.ravel()
for full documentation.参考
nlcpy.ravel
Equivalent function.
- repeat(self, repeats, axis=None)
Repeats elements of an array.
Refer to
nlcpy.repeat()
for full documentation.参考
nlcpy.repeat
Equivalent function.
- reshape(self, *shape, order='C')
Returns an array containing the same data with a new shape.
Refer to
nlcpy.reshape()
for full documentation.参考
nlcpy.reshape
Equivalent function.
注釈
Unlike the free function
nlcpy.reshape()
, this method onndarray.reshape()
allows the elements of the shape parameter to be passed in as separate arguments. For example,a.reshape(10, 11)
is equivalent toa.reshape((10, 11))
.
- resize(self, *new_shape, refcheck=True)
Changes shape and size of array in-place.
- Parameters
- new_shapetuple of ints, or n ints
Shape of resized array.
- refcheckbool, optional
If False, reference count will not be checked. Default is True.
- Returns
- None
参考
nlcpy.resize
Returns a new array with the specified shape.
注釈
This reallocates space for the data area if necessary.
Only contiguous arrays (data elements consecutive in memory) can be resized.
The purpose of the reference count check is to make sure you do not use this array as a buffer for another Python object and then reallocate the memory. However, reference counts can increase in other ways so if you are sure that you have not shared the memory for this array with another Python object, then you may safely set refcheck to False.
Examples
Shrinking an array: array is flattened (in the order that the data are stored in memory), resized, and reshaped:
>>> import nlcpy as vp >>> a = vp.array([[0, 1], [2, 3]], order='C') >>> a.resize((2, 1)) >>> a array([[0], [1]]) >>> a = vp.array([[0, 1], [2, 3]], order='F') >>> a.resize((2, 1)) >>> a array([[0], [2]])
Enlarging an array: as above, but missing entries are filled with zeros:
>>> b = vp.array([[0, 1], [2, 3]]) >>> b.resize(2, 3) # new_shape parameter doesn't have to be a tuple >>> b array([[0, 1, 2], [3, 0, 0]])
Referencing an array prevents resizing...
>>> c = a >>> a.resize((1, 1)) Traceback (most recent call last): ... ValueError: cannot resize an array that references or is referenced ...
Unless refcheck is False:
>>> a.resize((1, 2), refcheck=False) >>> a array([[0, 2]]) >>> c array([[0, 2]])
- set(self, a_cpu)
Copies an array on the host memory to
nlcpy.ndarray
.- Parameters
- a_cpunumpy.ndarray
The source array on the host memory.
- sort(self, axis=- 1, kind=None, order=None)
Sorts an array in-place.
Refer to
nlcpy.sort()
for full documentation.- Parameters
- axisint, optional
Axis along which to sort. Default is -1, which means sort along the last axis.
- kind{'stable'}, optional
Sorting algorithm.
- orderstr or list of str, optional
Not implemented.
参考
nlcpy.sort
Returns a sorted copy of an array.
nlcpy.argsort
Returns the indices that would sort an array.
制限事項
kind is not None and not 'stable' : NotImplementedError occurs.
order is not None : NotImplementedError occurs.
Examples
>>> import nlcpy as vp >>> a = vp.array([[1,4], [3,1]]) >>> a.sort(axis=1) >>> a array([[1, 4], [1, 3]]) >>> a.sort(axis=0) >>> a array([[1, 3], [1, 4]])
- squeeze(self, axis=None)
Removes single-dimensional entries from the shape of an array.
Refer to
nlcpy.squeeze()
for full documentation.参考
nlcpy.squeeze
Equivalent function.
- std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=nlcpy._NoValue)
Computes the standard deviation along the specified axis.
Returns the standard deviation, a measure of the spread of a distribution, of the array elements. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.
Refer to
nlcpy.std()
for full documentation.参考
nlcpy.std
Equivalent function.
- sum(self, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True)
Returns the sum of the array elements over the given axis.
参考
nlcpy.sum
Equivalent function.
- swapaxes(self, axis1, axis2)
Returns a view of the array with axis1 and axis2 interchanged.
Refer to
nlcpy.swapaxes()
for full documentation.参考
nlcpy.swapaxes
Equivalent function.
- take(self, indices, axis=None, out=None, mode='wrap') ndarray
Takes elements from an array along an axis.
Refer to
nlcpy.take()
for full documentation.参考
nlcpy.take
Equivalent function.
- tobytes(self, order='C')
Constructs Python bytes containing the raw data bytes in the array.
Constructs Python bytes showing a copy of the raw contents of data memory. The bytes object can be produced in either 'C' or 'Fortran', or 'Any' order (the default is 'C'-order). 'Any' order means C-order unless the F_CONTIGUOUS flag in the array is set, in which case it means 'Fortran' order.
- Parameters
- orderorder{'C', 'F', None}, optional
Order of the data for multidimensional arrays: C, Fortran, or the same as for the original array.
- Returns
- sbytes
Python bytes exhibiting a copy of a's raw data.
Examples
>>> import nlcpy as vp >>> x = vp.array([[0, 1], [2, 3]], dtype='<u4') >>> x.tobytes() b'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00' >>> x.tobytes('C') == x.tobytes() True >>> x.tobytes('F') b'\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00'
- tolist(self)
Returns the array as an
a.ndim
-levels deep nested list of Python scalars.Returns a copy of the array data as a (nested) Python list. Data items are converted to the nearest compatible builtin Python type, via the item function. If
a.ndim
is 0, then since the depth of the nested list is 0, it will not be a list at all, but a simple Python scalar.- Parameters
- none
- Returns
- yobject, or list of object, or list of list of object, or...
The possibly nested list of array elements.
注釈
The array may be recreated via
a = nlcpy.array(a.tolist())
, although this may sometimes lose precision.Examples
For a 1D array,
a.tolist()
is almost the same aslist(a)
:>>> import nlcpy as vp >>> a = vp.array([1, 2]) >>> list(a) [array(1), array(2)] >>> a.tolist() [1, 2]
However, for a 2D array,
tolist
applies recursively>>> a = vp.array([[1, 2], [3, 4]]) >>> list(a) [array([1, 2]), array([3, 4])] >>> a.tolist() [[1, 2], [3, 4]]
The base case for this recursion is a 0D array:
>>> a = vp.array(1) >>> list(a) Traceback (most recent call last): ... TypeError: iteration over a 0-d array >>> a.tolist() 1
- transpose(self, *axes)
Returns a view of the array with axes transposed.
For a 1-D array this has no effect, as a transposed vector is simply the same vector. To convert a 1-D array into a 2D column vector, an additional dimension must be added. a[:, nlcpy.newaxis] achieves this. For a 2-D array, this is a standard matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided and
a.shape = (i[0], i[1], ... i[n-2], i[n-1])
, thena.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])
.- Parameters
- axesNone, tuple of ints, or n ints
None or no argument: reverses the order of the axes.
tuple of ints: i in the j-th place in the tuple means a's i-th axis becomes a.transpose()'s j-th axis.
n ints: same as an n-tuple of the same ints (this form is intended simply as a "convenience" alternative to the tuple form)
- Returns
- outndarray
View of a, with axes suitably permuted.
参考
nlcpy.ndarray.reshape
Returns an array containing the same data with a new shape.
Examples
>>> import nlcpy as vp >>> a = vp.array([[1, 2], [3, 4]]) >>> a array([[1, 2], [3, 4]]) >>> a.transpose() array([[1, 3], [2, 4]]) >>> a.transpose((1, 0)) array([[1, 3], [2, 4]]) >>> a.transpose(1, 0) array([[1, 3], [2, 4]])
- var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=nlcpy._NoValue)
Computes the variance along the specified axis.
Returns the variance of the array elements, a measure of the spread of a distribution. The variance is computed for the flattened array by default, otherwise over the specified axis.
Refer to
nlcpy.var()
for full documentation.参考
nlcpy.var
Equivalent function.
- view(self, dtype=None, type=None) ndarray
Returns a new view of array with the same data.
- Parameters
- dtypedtype, optional
Data-type descriptor of the returned view, e.g., float32 or int64. The default, None, results in the view having the same data-type as a.
- typePython type, optional
Type of the returned view, e.g., ndarray or matrix. Again, omission of the parameter results in type preservation.
注釈
a.view()
is used two different ways:a.view(some_dtype)
ora.view(dtype=some_dtype)
constructs a view of the array's memory with a different dtype. This can cause a reinterpretation of the bytes of memory.For
a.view(some_dtype)
, ifsome_dtype
has a different number of bytes per entry than the previous dtype (for example, converting a regular array to a structured array), then the behavior of the view cannot be predicted just from the superficial appearance ofa
(shown byprint(a)
). It also depends on exactly howa
is stored in memory. Therefore if a is C-ordered versus fortran-ordered, versus defined as a slice or transpose, etc., the view may give different results.Examples
Viewing array data using a different dtype:
>>> import nlcpy as vp >>> x = vp.array([(1, 2)], dtype=vp.int32) >>> y = x.view(dtype=vp.int64) >>> y array([[8589934593]])
Making changes to the view changes the underlying array
>>> x = vp.array([(1, 2),(3,4)], dtype=vp.int32) >>> xv = x.view(dtype=vp.int32).reshape(-1,2) >>> xv[0,1] = 20 >>> x array([[ 1, 20], [ 3, 4]], dtype=int32)
- __eq__(value, /)
Return self==value.
- __ne__(value, /)
Return self!=value.
- __lt__(value, /)
Return self<value.
- __le__(value, /)
Return self<=value.
- __gt__(value, /)
Return self>value.
- __ge__(value, /)
Return self>=value.
- __bool__(/)
self != 0
Attributes
- T
Transposed array.
- base
- dtype
- flags
Information about the memory layout of the array.
- imag
Imaginary part.
- itemsize
- nbytes
Total number of bytes for all elements.
- ndim
Number of dimensions.
- node_id
- real
Real part.
- shape
Lengths of each axis.
- size
- strides
Strides of each axis in bytes.
- ve_adr
- venode
VENode object that ndarray exists on.
- veo_hmem