NumPy迭代數(shù)組的實(shí)現(xiàn)
迭代數(shù)組
NumPy中引入了 nditer 對(duì)象來提供一種對(duì)于數(shù)組元素的訪問方式。
一、單數(shù)組迭代
1. 使用 nditer 訪問數(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)存布局一致的, # 這樣做是為了提升訪問的效率,默認(rèn)是行序優(yōu)先(row-major order,或者說是 C-order)。 # 這反映了默認(rèn)情況下只需訪問每個(gè)元素,而無需考慮其特定順序。 # 我們可以通過迭代上述數(shù)組的轉(zhuǎn)置來看到這一點(diǎn), # 并與以 C 順序訪問數(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 控制元素的訪問順序,參數(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' 來標(biāo)志為讀寫或只讀模式。
此時(shí),nditer 在迭代時(shí)將生成可寫的緩沖區(qū)數(shù)組,可以在此進(jìn)行修改。為了在修改后,可以將修改的數(shù)據(jù)回寫到原始位置,需要在迭代結(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),跟蹤索引或多索引
以上操作在迭代過程中,都是逐元素進(jìn)行的,這雖然簡(jiǎ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
5. 以特定數(shù)據(jù)類型迭代
當(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ù)組迭代
如果不同形狀的數(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)文章
python檢測(cè)遠(yuǎn)程udp端口是否打開的方法
這篇文章主要介紹了python檢測(cè)遠(yuǎn)程udp端口是否打開的方法,涉及Python操作socket實(shí)現(xiàn)檢測(cè)udp端口的技巧,需要的朋友可以參考下2015-03-03
python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(一)K近鄰法
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)第一篇,K近鄰法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
python實(shí)現(xiàn)學(xué)生通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)學(xué)生通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
django+echart數(shù)據(jù)動(dòng)態(tài)顯示的例子
今天小編就為大家分享一篇django+echart數(shù)據(jù)動(dòng)態(tài)顯示的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】
這篇文章主要介紹了Python爬蟲爬取新浪微博內(nèi)容,結(jié)合實(shí)例形式分析了Python基于代理IP實(shí)現(xiàn)的微博爬取與抓包分析相關(guān)操作技巧,需要的朋友可以參考下2018-08-08

