14_NumPy数组ndarray的显示格式(位数,指数符号,零填充等)的指定

通过print()显示NumPy数组ndarray时,可以通过numpy.set_printoptions()来更改显示格式(位数,指数符号,零填充等)。

设置后,脚本将使用该设置。

np.set_printoptions()的设置是通过print()显示时的设置,原始ndarary本身的值不会更改。

在此:

  • 指定小数点后的位数(precision, floatmode)
  • 使用/不使用指数表示法(suppress)
  • 各种类型的格式指定(formatter)
    • 指定小数点后的位数
    • 指数
    • 整数零填充

对以上内容进行说明。

指定小数点后的位数(precision, floatmode)

precision

使用参数precision指定小数点后的位数。

可以使用np.get_printoptions()显示当前设置。每个参数的值以字典dict格式返回。

import numpy as np

print(np.get_printoptions())
# {'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 8, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': None, 'legacy': False}

precision是小数点后的位数的设置,而不是有效位数的设置,所以它不影响整数部分。 默认值为precision = 8。

a = np.array([12.3456, 0.123456789])
print(a)
# [12.3456      0.12345679]

np.set_printoptions(precision=3)
print(a)
# [12.346  0.123]

np.set_printoptions(precision=10)
print(a)
# [12.3456       0.123456789]

floatmode

小数点后的显示格式可以通过参数floatmode选择。 默认值为floatmode = maxprec。每个元素都显示有所需的位数。尾部不填充零。

np.set_printoptions(precision=4, floatmode='maxprec')
print(a)
# [12.3456  0.1235]

np.set_printoptions(precision=10, floatmode='maxprec')
print(a)
# [12.3456       0.123456789]

在floatmode = fixed中,所有元素的位数固定为精度值。尾部零填充。

np.set_printoptions(precision=4, floatmode='fixed')
print(a)
# [12.3456  0.1235]

np.set_printoptions(precision=10, floatmode='fixed')
print(a)
# [12.3456000000  0.1234567890]

在floatmode = maxprec_equal中,其他元素用零填充到具有最大位数的元素上。

np.set_printoptions(precision=4, floatmode='maxprec_equal')
print(a)
# [12.3456  0.1235]

np.set_printoptions(precision=10, floatmode='maxprec_equal')
print(a)
# [12.345600000  0.123456789]

floatmode = unique会显示每个元素所需的位数,而与精度值无关。

np.set_printoptions(precision=4, floatmode='unique')
print(a)
# [12.3456       0.123456789]

np.set_printoptions(precision=10, floatmode='unique')
print(a)
# [12.3456       0.123456789]

np.round()

如上所述,np.set_printoptions()的设置是通过print()显示时的设置,原始ndarary本身的值不变,并且不会生成新的ndarray。

如果要生成一个新的ndarray,它舍入为任意位数,使用np.round()。

在np.round()中,第一个参数指定目标ndarray,第二个参数指定小数点后的位数。如果对数字位数使用负值,则也可以四舍五入为整数位数。

b = np.round(a, 2)
print(b)
# [12.35  0.12]

b = np.round(a, -1)
print(b)
# [10.  0.]

b = np.round([1234.56, 123456.789], -2)
print(b)
# [  1200. 123500.]

使用/不使用指数表示法(suppress)

默认情况下,当ndarray的最小值小于1e-4(= 0.0001)或最大值与最小值之比大于1e3(= 1000)时,将以指数符号显示。 当采用指数表示法时,所有元素都采用指数表示法。

a = np.array([0.123456, 0.123456])
print(a)
# [0.123456 0.123456]

a = np.array([0.123456, 0.0000123456])
print(a)
# [1.23456e-01 1.23456e-05]

a = np.array([123.456, 0.0123456])
print(a)
# [1.23456e+02 1.23456e-02]

如果np.set_printoptions()中的参数个体=真,则禁止使用指数表示法,并且始终以十进制显示。默认值为抑制=假(带有指数表示法)

np.set_printoptions(suppress=True)
print(a)
# [123.456       0.0123456]

对于指数表示法,精度和浮点模式设置对于尾数有效。

np.set_printoptions(suppress=True, precision=2)
print(a)
# [123.46   0.01]

np.set_printoptions(suppress=False, precision=2)
print(a)
# [1.23e+02 1.23e-02]

np.set_printoptions(suppress=False, precision=8, floatmode='fixed')
print(a)
# [1.23456000e+02 1.23456000e-02]

各种类型的格式指定(formatter)

在np.set_printoptions()中,可以在参数格式器中以字典格式指定要应用于每种类型(例如float和int)的函数。

例如对于浮点数,.2f或.8f可以指定小数点后的位数,或者.2e或.8e可以指定尾数的尾数(以指数表示法)。

np.set_printoptions(precision=8, floatmode='maxprec', suppress=False)

a = np.array([123.456, 0.0123456])
print(a)
# [1.23456e+02 1.23456e-02]

np.set_printoptions(formatter={'float': '{:.2f}'.format})
print(a)
# [123.46 0.01]

np.set_printoptions(formatter={'float': '{:.8f}'.format})
print(a)
# [123.45600000 0.01234560]

np.set_printoptions(formatter={'float': '{:.2e}'.format})
print(a)
# [1.23e+02 1.23e-02]

np.set_printoptions(formatter={'float': '{:.8e}'.format})
print(a)
# [1.23456000e+02 1.23456000e-02]

可以将整数int填充为零,如08d所示,或转换为二进制b,八进制o或十六进制x进行显示。

a = np.array([12, 1234])
print(a)
# [  12 1234]

np.set_printoptions(formatter={'int': '{:08d}'.format})
print(a)
# [00000012 00001234]

np.set_printoptions(formatter={'int': '{:b}'.format})
print(a)
# [1100 10011010010]

np.set_printoptions(formatter={'int': '{:o}'.format})
print(a)
# [14 2322]

np.set_printoptions(formatter={'int': '{:x}'.format})
print(a)
# [c 4d2]

还可以使用字符串方法使字符串numpystr全部大写。也可以使用匿名函数(lambda表达式)执行任意处理。

a = np.array(['One', 'Two'])
print(a)
# ['One' 'Two']

np.set_printoptions(formatter={'numpystr': str.upper})
print(a)
# [ONE TWO]

np.set_printoptions(formatter={'numpystr': lambda x: '***' + x + '***'})
print(a)
# [***One*** ***Two***]

格式化程序设置可以以字典形式共同指定。如果有您总是要使用的设置,请在脚本的开头将其写入。

注意,不是为每个元素而是为ndarray设置类型dtype(例如float和int),并且该类型的格式化程序将应用于所有元素。

np.set_printoptions(formatter={'float': '{:0.8e}'.format, 'int': '{:08d}'.format})

a = np.array([12, 12.34])
print(a.dtype)
print(a)
# float64
# [1.20000000e+01 1.23400000e+01]

a = np.array([12, 123])
print(a.dtype)
print(a)
# int64
# [00000012 00000123]
点击阅读全文
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐