本文小編為大家詳細介紹“NumPy迭代數組如何實現”,內容詳細,步驟清晰,細節處理妥當,希望這篇“NumPy迭代數組如何實現”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
NumPy中引入了 nditer 對象來提供一種對于數組元素的訪問方式。
>>>a = np.arange(12).reshape(3, 4) >>>for x in np.nditer(a): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 # 以上實例不是使用標準 C 或者 Fortran 順序,選擇的順序是和數組內存布局一致的, # 這樣做是為了提升訪問的效率,默認是行序優先(row-major order,或者說是 C-order)。 # 這反映了默認情況下只需訪問每個元素,而無需考慮其特定順序。 # 我們可以通過迭代上述數組的轉置來看到這一點, # 并與以 C 順序訪問數組轉置的 copy 方式做對比,如下實例: >>>for x in np.nditer(a.T): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 >>>for x in np.nditer(a.T.copy(order='C')): print(x, end=' ') 0 4 8 1 5 9 2 6 10 3 7 11
使用參數 order 控制元素的訪問順序,參數的可選值有:
‘C’:C order,即是行序優先;
‘F’:Fortran order,即是列序優先;
’K’:參考數組元素在內存中的順序;
‘A’:表示’F’順序;
>>>a = np.arange(12).reshape(3, 4) >>>for x in np.nditer(a, order='C'): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 >>>a = np.arange(12).reshape(3, 4) >>>for x in np.nditer(a, order='F'): print(x, end=' ') 0 4 8 1 5 9 2 6 10 3 7 11 >>>a = np.arange(12).reshape(3, 4) >>>for x in np.nditer(a, order='K'): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 >>>a = np.arange(12).reshape(3, 4) >>>for x in np.nditer(a, order='A'): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11
在使用 nditer 對象迭代數組時,默認情況下是只讀狀態。因此,如果需要修改數組,可以使用參數 op_flags = 'readwrite' or 'writeonly' 來標志為讀寫或只讀模式。
此時,nditer 在迭代時將生成可寫的緩沖區數組,可以在此進行修改。為了在修改后,可以將修改的數據回寫到原始位置,需要在迭代結束后,拋出迭代結束信號,有兩種方式:
使用 with 上下文管理器;
在迭代結束后,調用迭代器的close方法;
>>>a = np.arange(12).reshape(3, 4) >>>print(a) >>>with np.nditer(a, op_flags=['readwrite']) as it: for x in it: x += 10 >>>print(a) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[10 11 12 13] [14 15 16 17] [18 19 20 21]]
以上操作在迭代過程中,都是逐元素進行的,這雖然簡單,但是效率不高??梢允褂脜?flags 讓 nditer 迭代時提供更大的塊。并可以通過強制設定 C 和 F 順序,得到不同的塊大小。
# 默認情況下保持本機的內存順序,迭代器提供單一的一維數組 # 'external_loop' 給出的值是具有多個值的一維數組,而不是零維數組 >>>a = np.arange(12).reshape(3, 4) >>>print(a) >>>for x in np.nditer(a, flags=['external_loop']): print(x, end=' ') [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [ 0 1 2 3 4 5 6 7 8 9 10 11], # 設定 'F' 順序 >>>a = np.arange(12).reshape(3, 4) >>>print(a) >>>for x in np.nditer(a, flags=['external_loop'], order='F'): print(x, end=' ') [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [0 4 8], [1 5 9], [ 2 6 10], [ 3 7 11], # 'c_index' 可以通過 it.index 跟蹤 'C‘ 順序的索引 >>>a = np.arange(12).reshape(3, 4) >>>print(a) >>>it = np.nditer(a, flags=['c_index']) >>>for x in it: print("{}: ({})".format(x, it.index)) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 0: (0) 1: (1) 2: (2) 3: (3) 4: (4) 5: (5) 6: (6) 7: (7) 8: (8) 9: (9) 10: (10) 11: (11) # 'f_index' 可以通過 it.index 跟蹤 'F‘ 順序的索引 >>>a = np.arange(12).reshape(3, 4) >>>print(a) >>>it = np.nditer(a, flags=['c_index']) >>>for x in it: print("{}: ({})".format(x, it.index)) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 0: (0) 1: (3) 2: (6) 3: (9) 4: (1) 5: (4) 6: (7) 7: (10) 8: (2) 9: (5) 10: (8) 11: (11) # 'multi_index' 可以通過 it.multi_index 跟蹤數組索引 >>>a = np.arange(12).reshape(3, 4) >>>print(a) >>>it = np.nditer(a, flags=['multi_index']) >>>for x in it: print("{}: {}".format(x, it.multi_index)) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 0: (0, 0) 1: (0, 1) 2: (0, 2) 3: (0, 3) 4: (1, 0) 5: (1, 1) 6: (1, 2) 7: (1, 3) 8: (2, 0) 9: (2, 1) 10: (2, 2) 11: (2, 3)
external_loop 與 multi_index、c_index、c_index不可同時使用,否則將引發錯誤 ValueError: Iterator flag EXTERNAL_LOOP cannot be used if an index or multi-index is being tracked
當需要以其它的數據類型來迭代數組時,有兩種方法:
臨時副本:迭代時,會使用新的數據類型創建數組的副本,然后在副本中完成迭代。但是,這種方法會消耗大量的內存空間。
緩沖模式: 使用緩沖來支持靈活輸入,內存開銷最小。
# 臨時副本 >>>a = np.arange(12).reshape(3, 4) >>>print(a.dtype) >>>it = np.nditer(a, op_flags=['readonly', 'copy'],op_dtypes=[np.float64]) >>>for x in it: print("{}".format(x), end=', ') int32 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, # 緩沖模式 >>>a = np.arange(12).reshape(3, 4) >>>print(a.dtype) >>>it = np.nditer(a, flags=['buffered'],op_dtypes=[np.float64]) >>>for x in it: print("{}".format(x), end=', ') int32 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,
注意
默認情況下,轉化會執行“安全”機制,如果不符合 NumPy 的轉換規則,會引發異常:TypeError: Iterator operand 0 dtype could not be cast from dtype('float64') to dtype('float32') according to the rule 'safe'
如果不同形狀的數組是可廣播的,那么 dtype 可以迭代多個數組。
>>> a = np.arange(3) >>> b = np.arange(6).reshape(2,3) >>> for x, y in np.nditer([a,b]): print("%d:%d" % (x,y), end=' ') 0:0 1:1 2:2 0:3 1:4 2:5
讀到這里,這篇“NumPy迭代數組如何實現”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。