NumPy迭代數(shù)組的實(shí)現(xiàn)
迭代數(shù)組
NumPy中引入了 nditer 對(duì)象來(lái)提供一種對(duì)于數(shù)組元素的訪問(wèn)方式。
一、單數(shù)組迭代
1. 使用 nditer 訪問(wèn)數(shù)組的每個(gè)元素
>>>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)存布局一致的, # 這樣做是為了提升訪問(wèn)的效率,默認(rèn)是行序優(yōu)先(row-major order,或者說(shuō)是 C-order)。 # 這反映了默認(rèn)情況下只需訪問(wèn)每個(gè)元素,而無(wú)需考慮其特定順序。 # 我們可以通過(guò)迭代上述數(shù)組的轉(zhuǎn)置來(lái)看到這一點(diǎn), # 并與以 C 順序訪問(wèn)數(shù)組轉(zhuǎn)置的 copy 方式做對(duì)比,如下實(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?
2. 控制數(shù)組元素的迭代順序
使用參數(shù) order 控制元素的訪問(wèn)順序,參數(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?
3. 修改數(shù)組值
在使用 nditer 對(duì)象迭代數(shù)組時(shí),默認(rèn)情況下是只讀狀態(tài)。因此,如果需要修改數(shù)組,可以使用參數(shù) op_flags = 'readwrite' or 'writeonly' 來(lái)標(biāo)志為讀寫(xiě)或只讀模式。
此時(shí),nditer 在迭代時(shí)將生成可寫(xiě)的緩沖區(qū)數(shù)組,可以在此進(jìn)行修改。為了在修改后,可以將修改的數(shù)據(jù)回寫(xiě)到原始位置,需要在迭代結(jié)束后,拋出迭代結(jié)束信號(hào),有兩種方式:
- 使用 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]]
4. 使用外部循環(huán),跟蹤索引或多索引
以上操作在迭代過(guò)程中,都是逐元素進(jìn)行的,這雖然簡(jiǎn)單,但是效率不高??梢允褂脜?shù) flags 讓 nditer 迭代時(shí)提供更大的塊。并可以通過(guò)強(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' 可以通過(guò) 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' 可以通過(guò) 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' 可以通過(guò) 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
5. 以特定數(shù)據(jù)類(lèi)型迭代
當(dāng)需要以其它的數(shù)據(jù)類(lèi)型來(lái)迭代數(shù)組時(shí),有兩種方法:
- 臨時(shí)副本:迭代時(shí),會(huì)使用新的數(shù)據(jù)類(lèi)型創(chuàng)建數(shù)組的副本,然后在副本中完成迭代。但是,這種方法會(huì)消耗大量的內(nèi)存空間。
- 緩沖模式: 使用緩沖來(lái)支持靈活輸入,內(nèi)存開(kāi)銷(xiāo)最小。
# 臨時(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ù)組迭代
如果不同形狀的數(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
到此這篇關(guān)于NumPy迭代數(shù)組的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)NumPy迭代數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch創(chuàng)建tensor函數(shù)詳情
這篇文章主要介紹了pytorch創(chuàng)建tensor函數(shù)詳情,文章圍繞tensor函數(shù)的相關(guān)自來(lái)哦展開(kāi)詳細(xì)內(nèi)容的介紹,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-03-0310種檢測(cè)Python程序運(yùn)行時(shí)間、CPU和內(nèi)存占用的方法
這篇文章主要介紹了10種檢測(cè)Python程序運(yùn)行時(shí)間、CPU和內(nèi)存占用的方法,包括利用Python裝飾器或是外部的Unix Shell命令等,需要的朋友可以參考下2015-04-04python Tcp協(xié)議發(fā)送和接收信息的例子
今天小編就為大家分享一篇python Tcp協(xié)議發(fā)送和接收信息的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07Django?ORM?F對(duì)象和Q對(duì)象查詢(xún)
Django提供了兩個(gè)非常有用的工具:F對(duì)象和Q對(duì)象,方便了在一些特殊場(chǎng)景下的查詢(xún)過(guò)程,這篇文章主要介紹了Django?ORM?F對(duì)象和Q對(duì)象查詢(xún),需要的朋友可以參考下2022-10-10Python import與from import使用及區(qū)別介紹
Python程序可以調(diào)用一組基本的函數(shù)(即內(nèi)建函數(shù)),比如print()、input()和len()等函數(shù)。接下來(lái)通過(guò)本文給大家介紹Python import與from import使用及區(qū)別介紹,感興趣的朋友一起看看吧2018-09-09python中@contextmanager裝飾器的用法詳解
這篇文章主要介紹了python中@contextmanager裝飾器的用法詳解,@contextmanager 的作用就是我們可以把一個(gè)非自定義類(lèi)改成一個(gè)上下文管理器,需要的朋友可以參考下2023-07-07docker django無(wú)法訪問(wèn)redis容器的解決方法
今天小編就為大家分享一篇docker django無(wú)法訪問(wèn)redis容器的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08