[Python] Numpy數組格式化打印 (指定小數點位數)


Numpy數組格式化打印方法 (指定小數點位數)np.set_printoptions(precision=3, suppress=True)
precision: 保留幾位小數,后面不會補0
supress: 對很大/小的數不使用科學計數法 (true)
formatter: 強制格式化,后面會補0

代碼:

import numpy as np
a = np.random.random(3)
print('before set precision: \n',a)

np.set_printoptions(precision=3, suppress=True)
print('after set precision: \n',a)

np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
print('after set formatter: \n',a)

結果:

before set options: 
 [ 0.05856348 0.5400039 0.70000603]
after set precision: 
 [ 0.059 0.54 0.7]
after set formatter: 
 [ 0.059 0.540 0.700]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM