numpy API:np.set_printoptions

作用:确定浮点数字、数组、和numpy对象的显示形式。
例子:

#精度为小数点后4位
np.set_printoptions(precision=4)
print(np.array([1.123456789]))

[1.1235]

#超过阈值就缩写
np.set_printoptions(threshold=4)
print(np.arange(10))

[0 1 2 … 7 8 9]

eps = np.finfo(float).eps #np.finfo:用电脑的float极限长度
eps

2.220446049250313e-16

x = np.arange(4.)
x

array([0., 1., 2., 3.])

x**2 - (x + eps)**2

array([-4.9304e-32, -4.4409e-16, 0.0000e+00, 0.0000e+00])

#过小的结果会被压缩
np.set_printoptions(suppress=True)
x**2 - (x + eps)**2

array([-0., -0., 0., 0.])

#用户设定数组元素的显示形式
np.set_printoptions(formatter={'all':lambda x: '囧: '+str(-x)})#所有元素应用一个lambda函数
x = np.arange(3)
x

array([囧: 0, 囧: -1, 囧: -2])

#设定为默认设置
np.set_printoptions(edgeitems=3,infstr='inf',
                    linewidth=75, nanstr='nan', precision=8,
                    suppress=False, threshold=1000, formatter=None)
x = np.arange(3)
x

array([0, 1, 2])

猜你喜欢

转载自blog.csdn.net/nockinonheavensdoor/article/details/80328074