numpy.ndarray.view()(懵逼,看不太懂???)(view不会开辟新的内存空间)

from numpy\core_multiarray_umath.py

def view(self, dtype=None, type=None): # real signature unknown; restored from __doc__
    """
    a.view(dtype=None, type=None)
    
        New view of array with the same data.
        具有相同数据的数组的新视图。
    
        Parameters
        ----------
        dtype : data-type or ndarray sub-class, optional
            Data-type descriptor of the returned view, e.g., float32 or int16. The
            default, None, results in the view having the same data-type as `a`.
            This argument can also be specified as an ndarray sub-class, which
            then specifies the type of the returned object (this is equivalent to
            setting the ``type`` parameter).

			数据类型或ndarray子类,可选

返回视图的数据类型描述符,例如float32或int16。 

默认值为None(无),导致视图具有与a相同的数据类型。 

此参数也可以指定为ndarray子类,然后指定返回对象的类型(这等效于设置“ type”参数)。
			
        type : Python type, optional
            Type of the returned view, e.g., ndarray or matrix.  Again, the
            default None results in type preservation.
	
			返回视图的类型,例如ndarray或matrix。 同样,默认值None将导致类型保留。
    
        Notes
        -----
        ``a.view()`` is used two different ways:
    
        ``a.view(some_dtype)`` or ``a.view(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.
    
        ``a.view(ndarray_subclass)`` or ``a.view(type=ndarray_subclass)`` just
        returns an instance of `ndarray_subclass` that looks at the same array
        (same shape, dtype, etc.)  This does not cause a reinterpretation of the
        memory.
    
        For ``a.view(some_dtype)``, if ``some_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 of ``a`` (shown
        by ``print(a)``). It also depends on exactly how ``a`` 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.

		a.view()有两种不同的用法:
    
         a.view(some_dtype)或a.view(dtype = some_dtype)构造具有不同数据类型的阵列内存视图。 这可能导致对内存字节的重新解释。
    
         a.view(ndarray_subclass)或a.view(type = ndarray_subclass)只是返回一个ndarray_subclass实例,该实例看相同的数组(形状,dtype等)。这不会导致 记忆的重新诠释。
    
         对于a.view(some_dtype),如果some_dtype每个条目的字节数与上一个dtype不同(例如,将常规数组转换为结构化数组),则视图的行为 不能仅从``a''的表面外观(由``print(a)``所示)来预测。 这也完全取决于``a''在内存中的存储方式。 因此,如果``a''是C顺序相对于fortran顺序,相对于定义为切片或转置等,则视图可能会给出不同的结果。
    
    
        Examples
        --------
        >>> x = np.array([(1, 2)], dtype=[('a', np.int8), ('b', np.int8)])
    
        Viewing array data using a different type and dtype:
        使用不同的type和dtype查看数组数据:
    
        >>> y = x.view(dtype=np.int16, type=np.matrix)
        >>> y
        matrix([[513]], dtype=int16)
        >>> print(type(y))
        <class 'numpy.matrix'>

		(↑↑↑↑↑ 啥玩意,没看懂???)
    
        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)
        >>> xv
        array([[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
        >>> x
        array([(1, 20), (3,  4)], dtype=[('a', 'i1'), ('b', 'i1')])
    
        Using a view to convert an array to a recarray:
        使用视图将数组转换为RecArray:
    
        >>> z = x.view(np.recarray)
        >>> z.a
        array([1, 3], dtype=int8)
    
        Views share data:
    	视图共享数据:
    	
        >>> x[0] = (9, 10)
        >>> z[0]
        (9, 10)
    
        Views that change the dtype size (bytes per entry) should normally be
        avoided on arrays defined by slices, transposes, fortran-ordering, etc.:
        
        通常应在由切片,转置,fortran顺序等定义的数组上避免更改dtype大小
        (每个条目的字节数)的视图:
    
        >>> x = np.array([[1,2,3],[4,5,6]], dtype=np.int16)
        >>> y = x[:, 0:2]
        >>> y
        array([[1, 2],
               [4, 5]], 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 array must be C-contiguous
        >>> z = y.copy()
        >>> z.view(dtype=[('width', np.int16), ('length', np.int16)])
        array([[(1, 2)],
               [(4, 5)]], dtype=[('width', '<i2'), ('length', '<i2')])
    """
    pass
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值