[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]
posted @ 2019-01-08 16:53  herryzz  阅读(17559)  评论(0编辑  收藏  举报