numpy.ndarray.view()(懵逼,看不太懂???)(view不会开辟新的内存空间)
生活随笔
收集整理的這篇文章主要介紹了
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, optionalData-type descriptor of the returned view, e.g., float32 or int16. Thedefault, None, results in the view having the same data-type as `a`.This argument can also be specified as an ndarray sub-class, whichthen specifies the type of the returned object (this is equivalent tosetting the ``type`` parameter).數據類型或ndarray子類,可選返回視圖的數據類型描述符,例如float32或int16。 默認值為None(無),導致視圖具有與a相同的數據類型。 此參數也可以指定為ndarray子類,然后指定返回對象的類型(這等效于設置“ type”參數)。type : Python type, optionalType of the returned view, e.g., ndarray or matrix. Again, thedefault 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 viewof the array's memory with a different data-type. This can cause areinterpretation of the bytes of memory.``a.view(ndarray_subclass)`` or ``a.view(type=ndarray_subclass)`` justreturns an instance of `ndarray_subclass` that looks at the same array(same shape, dtype, etc.) This does not cause a reinterpretation of thememory.For ``a.view(some_dtype)``, if ``some_dtype`` has a different number ofbytes per entry than the previous dtype (for example, converting aregular array to a structured array), then the behavior of the viewcannot be predicted just from the superficial appearance of ``a`` (shownby ``print(a)``). It also depends on exactly how ``a`` is stored inmemory. Therefore if ``a`` is C-ordered versus fortran-ordered, versusdefined as a slice or transpose, etc., the view may give differentresults.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)>>> ymatrix([[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)>>> 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:使用視圖將數組轉換為RecArray:>>> z = x.view(np.recarray)>>> z.aarray([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 beavoided 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]>>> yarray([[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總結
以上是生活随笔為你收集整理的numpy.ndarray.view()(懵逼,看不太懂???)(view不会开辟新的内存空间)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python id()函数(返回对象在内
- 下一篇: pytorch torch.from_n