您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“NumPy迭代數(shù)組如何實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“NumPy迭代數(shù)組如何實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
NumPy中引入了 nditer 對象來提供一種對于數(shù)組元素的訪問方式。
>>>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 # 以上實(shí)例不是使用標(biāo)準(zhǔn) C 或者 Fortran 順序,選擇的順序是和數(shù)組內(nèi)存布局一致的, # 這樣做是為了提升訪問的效率,默認(rèn)是行序優(yōu)先(row-major order,或者說是 C-order)。 # 這反映了默認(rèn)情況下只需訪問每個(gè)元素,而無需考慮其特定順序。 # 我們可以通過迭代上述數(shù)組的轉(zhuǎn)置來看到這一點(diǎn), # 并與以 C 順序訪問數(shù)組轉(zhuǎn)置的 copy 方式做對比,如下實(shí)例: >>>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
使用參數(shù) order 控制元素的訪問順序,參數(shù)的可選值有:
‘C’:C order,即是行序優(yōu)先;
‘F’:Fortran order,即是列序優(yōu)先;
’K’:參考數(shù)組元素在內(nèi)存中的順序;
‘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 對象迭代數(shù)組時(shí),默認(rèn)情況下是只讀狀態(tài)。因此,如果需要修改數(shù)組,可以使用參數(shù) op_flags = 'readwrite' or 'writeonly' 來標(biāo)志為讀寫或只讀模式。
此時(shí),nditer 在迭代時(shí)將生成可寫的緩沖區(qū)數(shù)組,可以在此進(jìn)行修改。為了在修改后,可以將修改的數(shù)據(jù)回寫到原始位置,需要在迭代結(jié)束后,拋出迭代結(jié)束信號,有兩種方式:
使用 with 上下文管理器;
在迭代結(jié)束后,調(diào)用迭代器的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]]
以上操作在迭代過程中,都是逐元素進(jìn)行的,這雖然簡單,但是效率不高??梢允褂脜?shù) flags 讓 nditer 迭代時(shí)提供更大的塊。并可以通過強(qiáng)制設(shè)定 C 和 F 順序,得到不同的塊大小。
# 默認(rèn)情況下保持本機(jī)的內(nèi)存順序,迭代器提供單一的一維數(shù)組 # 'external_loop' 給出的值是具有多個(gè)值的一維數(shù)組,而不是零維數(shù)組 >>>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], # 設(shè)定 '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 跟蹤數(shù)組索引 >>>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不可同時(shí)使用,否則將引發(fā)錯(cuò)誤 ValueError: Iterator flag EXTERNAL_LOOP cannot be used if an index or multi-index is being tracked
當(dāng)需要以其它的數(shù)據(jù)類型來迭代數(shù)組時(shí),有兩種方法:
臨時(shí)副本:迭代時(shí),會(huì)使用新的數(shù)據(jù)類型創(chuàng)建數(shù)組的副本,然后在副本中完成迭代。但是,這種方法會(huì)消耗大量的內(nèi)存空間。
緩沖模式: 使用緩沖來支持靈活輸入,內(nèi)存開銷最小。
# 臨時(shí)副本 >>>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,
注意
默認(rèn)情況下,轉(zhuǎn)化會(huì)執(zhí)行“安全”機(jī)制,如果不符合 NumPy 的轉(zhuǎn)換規(guī)則,會(huì)引發(fā)異常:TypeError: Iterator operand 0 dtype could not be cast from dtype('float64') to dtype('float32') according to the rule 'safe'
如果不同形狀的數(shù)組是可廣播的,那么 dtype 可以迭代多個(gè)數(shù)組。
>>> 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迭代數(shù)組如何實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。