numpy 学习汇总34 - set_printoptions设置输出样式( 初步学习 tcy)

np.set_printoptions 2018/11/4  2018/11/28

 

set_printoptions(threshod='nan') #打印整个数组
np.set_printoptions(precision=4,suppress=True) #设置浮点精度
print(np.array([1.123456789])) # [ 1.1235]  

其他实 

set_printoptions(threshod='nan') #打印整个数组
np.set_printoptions(precision = 4) #设置浮点精度
print(np.array([1.123456789])) # [1.1235]
np.set_printoptions(edgeitems=3,infstr='inf',linewidth=75, nanstr='nan',
precision=8,suppress=False, threshold=1000, formatter=None)#返回默认选项

np.set_printoptions(threshold=5) #概略显示
print(np.arange(10)) # [0 1 2 ..., 7 8 9]

# Small results can be suppressed:
eps = np.finfo(float).eps
x = np.arange(4.)
x--2 - (x + eps)--2 # array([4., 4., 4., 4.])
np.set_printoptions(suppress=True)
x--2 - (x + eps)--2 # array([4., 4., 4., 4.])

# 自定义格式化:根据需要显示数组元素
np.set_printoptions(formatter={'all':lambda x: 'int: '+str(-x)})
x = np.arange(3)
x # array([int: 0, int: -1, int: -2])
np.set_printoptions() # formatter gets reset
x # array([0, 1, 2])

其他实例-with

# set_printoptions设置仅用在上下文中:跳出with代码块恢复默认打印参数
import numpy as np
from contextlib import contextmanager

@contextmanager
def printoptions(*args, **kwargs):
original_options = np.get_printoptions()
np.set_printoptions(*args, **kwargs)
try:
yield
finally:
np.set_printoptions(**original_options)

x = np.random.random(3)
y = np.array([1.5e-2, 1.5, 1500])

print('x = {}'.format(x))# x = [0.67779646 0.64912079 0.82931084]
print('y = {}'.format(y))# y = [1.5e-02 1.5e+00 1.5e+03]

with printoptions(precision=3, suppress=True):
print('x = {}'.format(x))# x = [0.678 0.649 0.829]
print('y = {}'.format(y))# y = [ 0.015 1.5 1500. ]

print('x = {}'.format(x))# x = [0.67779646 0.64912079 0.82931084]
print('y = {}'.format(y))# y = [1.5e-02 1.5e+00 1.5e+03]

 

备注:

np.set_printoptions(precision=None, threshold=None, edgeitems=None,
               linewidth=None, suppress=None, nanstr=None, infstr=None,
               formatter=None, sign=None, floatmode=None, --kwarg) #
设置打印选项

              

参数

precision:int or None浮点输出的精度位数(默认8)# 如floatmode不是fixed,可能是None

threshold:int触发汇总的数组元素总数而不是完整的repr(默认1000)
edgeitems:int在开头和结尾的摘要中的数组项数 每个维度(默认为3)
linewidth:int每行用于插入的字符数# 换行符(默认为75)
suppress : bool,科学记数法启用
    # True用固定点打印浮点数符号,当前精度中的数字等于零将打印为零。
    # False用科学记数法;最小数绝对值是<1e-4或比率最大绝对值> 1e3。默认值False
nanstr:str浮点非字母数字的字符串表示形式(默认为nan)
infstr:str浮点无穷大字符串表示形式(默认inf)
sign:string,' - ''+''',控制浮点类型符号的打印。
    # '+'打印正值标志。''打印空格。' - '省略正值符号,默认
 
formatter:可调用字典,格式化功能
    # 格式化设置类型:
     - 'bool'
    
- 'int'
    
- 'timedelta''numpy.timedelta64'
    
- 'datetime':numpy.datetime64
     - 'float'
    
- 'longfloat':128位浮点数
     - 'complexfloat'
    
- 'longcomplexfloat':由两个128位浮点组成
     - 'numpystr' : types numpy.string_ and numpy.unicode_
     - 'object' : np.object_ arrays
     - 'str':所有其他字符串
   
    # 用于一次设置一组类型的其他键:
     - 'all':设置所有类型
     - 'int_kind':设置'int'
    
- 'float_kind':设置'float''longfloat'
    
- 'complex_kind':设置'complexfloat''longcomplexfloat'
    
- 'str_kind':设置'str''numpystr'
 

floatmode:str控制precision选项的解释
   # 浮点类型值:
    -'fixed':始终打印精确的'precision精度'小数位
    -'unique':打印最小小数位数,precision选项被忽略。
    -'maxprec':打印最多precision小数位数
    -'maxprec_equal':最多打印precision小数位数
 
legacy:string或False
    # 如为字符串“1.13”,则启用1.13传统打印模式。
    # 如设置“False”,禁用传统模式。无法识别的字符串将被忽略

==========================================================

也可以看看
 get_printoptions,set_string_function,array2string
备注:
      formatter总是通过调用set_printoptions重置。

你可能感兴趣的:(numpy)