這篇文章給大家分享的是有關(guān)Python Numpy控制臺完全輸出ndarray的實現(xiàn)示例的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
如下所示:
import numpy as np np.set_printoptions(threshold=np.nan) print(ndarray)
當(dāng)ndarray里面的存放的數(shù)據(jù)維度過大時,在控制臺會出現(xiàn)不能將ndarray完全輸出的情況,中間部分的結(jié)果會用省略號打印出來。這時就需要用到numpy里面的set_printoptions()方法。
set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None)
precision:輸出結(jié)果保留精度的位數(shù)
threshold:array數(shù)量的個數(shù)在小于threshold的時候不會被折疊
edgeitems:在array已經(jīng)被折疊后,開頭和結(jié)尾都會顯示edgeitems個數(shù)
formatter:這個很有意思,像python3里面str.format(),就是可以對你的輸出進行自定義的格式化
其他的暫時沒用到
舉例:
precision: np.set_printoptions(precision=4) print(np.array([1.23456789])) >> [ 1.2346] # 最后進位了
threshold: np.set_printoptions(threshold=10) print(np.arange(1, 11, 1)) # np.arange(1, 11, 1)生成出來是[1-10],10個數(shù) >> [ 1 2 3 4 5 6 7 8 9 10] np.set_printoptions(threshold=9) print(np.arange(1, 11, 1)) >> [ 1 2 3 ..., 8 9 10]
edgeitems: np.set_printoptions(threshold=5) print(np.arange(1, 11, 1)) >> [ 1 2 3 ..., 8 9 10] np.set_printoptions(threshold=5, edgeitems=4) print(np.arange(1, 11, 1)) >> [ 1 2 3 4 ..., 7 8 9 10]
formatter np.set_printoptions(formatter={'all': lambda x: 'int: ' + str(-x)}) print(np.arange(1, 5, 1)) >> [int: -1 int: -2 int: -3 int: -4]
這個formatter是一個可調(diào)用的字典,'all'是其中一個key,表示里面的x可以包含所有type,還有其他key,具體可以在源碼里面查看
最后如果只想在代碼中的某一部分使用自定義的printoptions,那么可以通過再次調(diào)用np.set_printoptions()這個方法來進行reset
感謝各位的閱讀!關(guān)于“Python Numpy控制臺完全輸出ndarray的實現(xiàn)示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)頁題目:PythonNumpy控制臺完全輸出ndarray的實現(xiàn)示例-創(chuàng)新互聯(lián)
文章鏈接:http://aaarwkj.com/article40/dsphho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站導(dǎo)航、電子商務(wù)、定制開發(fā)、App開發(fā)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容