dpnp.ndarray.view
method
- ndarray.view(dtype=None, *, type=None)
New view of array with the same data.
For full documentation refer to
numpy.ndarray.view.- Parameters:
dtype ({None, str, dtype object, type}, optional) --
The desired data type of the returned view, e.g.
dpnp.float32ordpnp.int16. Omitting it results in the view having the same data type. Can also be a subclass ofdpnp.ndarrayto create a view of that type (this is equivalent to setting the type parameter).Default:
None.type ({None, type}, optional) --
Type of the returned view, e.g. a subclass of
dpnp.ndarray. If specified, the returned array will be an instance of type. Omitting it results in type preservation.Default:
None.
Notes
Passing
Nonefor dtype is the same as omitting the parameter, opposite to NumPy where they have different meaning.view(some_dtype)orview(dtype=some_dtype)constructs a view of the array's memory with a different data type. This can cause a reinterpretation of the bytes of memory.Only the last axis has to be contiguous.
Examples
>>> import dpnp as np >>> x = np.ones((4,), dtype=np.float32) >>> xv = x.view(dtype=np.int32) >>> xv[:] = 0 >>> xv array([0, 0, 0, 0], dtype=int32)
However, views that change dtype are totally fine for arrays with a contiguous last axis, even if the rest of the axes are not C-contiguous:
>>> x = np.arange(2 * 3 * 4, dtype=np.int8).reshape(2, 3, 4) >>> x.transpose(1, 0, 2).view(np.int16) array([[[ 256, 770], [3340, 3854]], [[1284, 1798], [4368, 4882]], [[2312, 2826], [5396, 5910]]], dtype=int16)
Creating a view with a custom ndarray subclass:
>>> class MyArray(np.ndarray): ... pass >>> x = np.array([1, 2, 3]) >>> y = x.view(MyArray) >>> type(y) <class 'MyArray'>