本文簡要介紹 python 語言中  numpy.ndarray.view  的用法。
- 用法:- ndarray.view([dtype][, type])
- 具有相同數據的數組的新視圖。 - 注意 - 為 - dtype傳遞 None 與省略參數不同,因為前者調用- dtype(None)這是- dtype('float_')的別名。- dtype: 數據類型或 ndarray sub-class,可選
- 返回視圖的數據類型說明符,例如 float32 或 int16。省略它會導致視圖具有與以下內容相同的數據類型a。該參數也可以指定為 ndarray sub-class,然後指定返回對象的類型(這相當於設置 - type範圍)。
- type: Python 類型,可選
- 返回視圖的類型,例如 ndarray 或矩陣。同樣,省略參數會導致類型保留。 
 
 - 參數:- 注意:- a.view()有兩種不同的使用方式:- a.view(some_dtype)或- a.view(dtype=some_dtype)使用不同的數據類型構造數組內存的視圖。這可能會導致重新解釋內存字節。- a.view(ndarray_subclass)或者- a.view(type=ndarray_subclass)隻返回一個實例ndarray_subclass查看相同的數組(相同的形狀、數據類型等),這不會導致內存的重新解釋。- 對於 - a.view(some_dtype),如果- some_dtype每個條目的字節數與之前的 dtype 不同(例如,將常規數組轉換為結構化數組),則無法僅從表麵的外觀預測視圖的行為- a(由- print(a)顯示)。它還取決於- a在內存中的存儲方式。因此,如果- a是C-ordered 與fortran-ordered,而不是定義為切片或轉置等,則視圖可能會給出不同的結果。- 例子:- >>> x = np.array([(1, 2)], dtype=[('a', np.int8), ('b', np.int8)])- 使用不同的類型和 dtype 查看數組數據: - >>> y = x.view(dtype=np.int16, type=np.matrix) >>> y matrix([[513]], dtype=int16) >>> print(type(y)) <class 'numpy.matrix'>- 在結構化數組上創建視圖,以便將其用於計算 - >>> 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.])- 對視圖進行更改會更改底層數組 - >>> xv[0,1] = 20 >>> x array([(1, 20), (3, 4)], dtype=[('a', 'i1'), ('b', 'i1')])- 使用視圖將數組轉換為recarray: - >>> z = x.view(np.recarray) >>> z.a array([1, 3], dtype=int8)- 視圖共享數據: - >>> x[0] = (9, 10) >>> z[0] (9, 10)- 通常應避免在由切片、轉置、fortran-ordering 等定義的數組上更改 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')])
相關用法
- Python numpy ndarray.astype用法及代碼示例
- Python numpy ndarray.flat用法及代碼示例
- Python numpy ndarray.setflags用法及代碼示例
- Python numpy ndarray.setfield用法及代碼示例
- Python numpy ndarray.sort用法及代碼示例
- Python numpy ndarray.real用法及代碼示例
- Python numpy ndarray.strides用法及代碼示例
- Python numpy ndarray.itemset用法及代碼示例
- Python numpy ndarray.__class_getitem__用法及代碼示例
- Python numpy ndarray.partition用法及代碼示例
- Python numpy ndarray.transpose用法及代碼示例
- Python numpy ndarray.flatten用法及代碼示例
- Python numpy ndarray.resize用法及代碼示例
- Python numpy ndarray.dtype用法及代碼示例
- Python numpy ndarray.imag用法及代碼示例
- Python numpy ndarray.dot用法及代碼示例
- Python numpy ndarray.size用法及代碼示例
- Python numpy ndarray.fill用法及代碼示例
- Python numpy ndarray.item用法及代碼示例
- Python numpy ndarray.nbytes用法及代碼示例
- Python numpy ndarray.tobytes用法及代碼示例
- Python numpy ndarray.copy用法及代碼示例
- Python numpy ndarray.ctypes用法及代碼示例
- Python numpy ndarray.shape用法及代碼示例
- Python numpy ndarray.base用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.ndarray.view。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
