numpy.matrix.view#

method

matrix.view([dtype][,type])#

New view of array with the same data.

Note

Passing None fordtype is different from omitting the parameter,since the former invokesdtype(None) which is an alias fordtype('float64').

Parameters:
dtypedata-type or ndarray sub-class, optional

Data-type descriptor of the returned view, e.g., float32 or int16.Omitting it results in the view having the same data-type asa.This argument can also be specified as an ndarray sub-class, whichthen specifies the type of the returned object (this is equivalent tosetting thetype parameter).

typePython type, optional

Type of the returned view, e.g., ndarray or matrix. Again, omissionof the parameter results in type preservation.

Notes

a.view() is used two different ways:

a.view(some_dtype) ora.view(dtype=some_dtype) constructs a viewof the array’s memory with a different data-type. This can cause areinterpretation of the bytes of memory.

a.view(ndarray_subclass) ora.view(type=ndarray_subclass) justreturns an instance ofndarray_subclass that looks at the same array(same shape, dtype, etc.) This does not cause a reinterpretation of thememory.

Fora.view(some_dtype), ifsome_dtype has a different number ofbytes per entry than the previous dtype (for example, converting a regulararray to a structured array), then the last axis ofa must becontiguous. This axis will be resized in the result.

Changed in version 1.23.0:Only the last axis needs to be contiguous. Previously, the entire arrayhad to be C-contiguous.

Examples

>>>importnumpyasnp>>>x=np.array([(-1,2)],dtype=[('a',np.int8),('b',np.int8)])

Viewing array data using a different type and dtype:

>>>nonneg=np.dtype([("a",np.uint8),("b",np.uint8)])>>>y=x.view(dtype=nonneg,type=np.recarray)>>>x["a"]array([-1], dtype=int8)>>>y.aarray([255], dtype=uint8)

Creating a view on a structured array so it can be used in calculations

>>>x=np.array([(1,2),(3,4)],dtype=[('a',np.int8),('b',np.int8)])>>>xv=x.view(dtype=np.int8).reshape(-1,2)>>>xvarray([[1, 2],       [3, 4]], dtype=int8)>>>xv.mean(0)array([2.,  3.])

Making changes to the view changes the underlying array

>>>xv[0,1]=20>>>xarray([(1, 20), (3,  4)], dtype=[('a', 'i1'), ('b', 'i1')])

Using a view to convert an array to a recarray:

>>>z=x.view(np.recarray)>>>z.aarray([1, 3], dtype=int8)

Views share data:

>>>x[0]=(9,10)>>>z[0]np.record((9, 10), dtype=[('a', 'i1'), ('b', 'i1')])

Views that change the dtype size (bytes per entry) should normally beavoided on arrays defined by slices, transposes, fortran-ordering, etc.:

>>>x=np.array([[1,2,3],[4,5,6]],dtype=np.int16)>>>y=x[:,::2]>>>yarray([[1, 3],       [4, 6]], dtype=int16)>>>y.view(dtype=[('width',np.int16),('length',np.int16)])Traceback (most recent call last):...ValueError:To change to a dtype of a different size, the last axis must be contiguous>>>z=y.copy()>>>z.view(dtype=[('width',np.int16),('length',np.int16)])array([[(1, 3)],       [(4, 6)]], dtype=[('width', '<i2'), ('length', '<i2')])

However, views that change dtype are totally fine for arrays with acontiguous 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)
On this page