zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python Numpy 中的打印设置函数set_printoptions

Pythonsetnumpy 函数 设置 打印
2023-09-11 14:17:10 时间
一 概述
np.set_printoptions()用于控制Python中小数的显示精度。
二 解析
np.set_printoptions(precision=None, threshold=None, linewidth=None, suppress=None, formatter=None)
1.precision:控制输出结果的精度(即小数点后的位数),默认值为8
2.threshold:当数组元素总数过大时,设置显示的数字位数,其余用省略号代替(当数组元素总数大于设置值,控制输出值得个数为6个,当数组元素小于或者等于设置值得时候,全部显示),当设置值为sys.maxsize(需要导入sys库),则会输出所有元素
3.linewidth:每行字符的数目,其余的数值会换到下一行
4.suppress:小数是否需要以科学计数法的形式输出
5.formatter:自定义输出规则
三 实例
1.浮点精度
import numpy as np

val=[1.2345678]
np.set_printoptions(precision=4)
print(np.array(val))
结果:
[1.2346]
2.显示
import numpy as np

val1=[1,2,3,4,5,6,7,8,9,0]
np.set_printoptions(threshold=10)
print(np.array(val1))
结果:
[1 2 3 4 5 6 7 8 9 0]