numpy打印参数设置
1 | numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None, sign=None, floatmode=None, *, legacy=None) |
precisionint or None, optional float输出的精度,即小数点后维数,默认8
Number of digits of precision for floating point output (default 8). May be None if floatmode is not fixed, to print as many digits as necessary to uniquely specify the value.
thresholdint, optional 当数组数目过大时,设置显示几个数字,其余用省略号
Total number of array elements which trigger summarization rather than full repr (default 1000). To always use the full repr without summarization, pass
sys.maxsize
.edgeitemsint, optional 边缘数目
Number of array items in summary at beginning and end of each dimension (default 3).
linewidthint, optional 每行字符数
The number of characters per line for the purpose of inserting line breaks (default 75).
suppressbool, optional 是否压缩由科学计数法表示的浮点数
If True, always print floating point numbers using fixed point notation, in which case numbers equal to zero in the current precision will print as zero. If False, then scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3. The default is False.
nanstrstr, optional
String representation of floating point not-a-number (default nan).
infstrstr, optional
String representation of floating point infinity (default inf).
signstring, either ‘-‘, ‘+’, or ‘ ‘, optional
Controls printing of the sign of floating-point types. If ‘+’, always print the sign of positive values. If ‘ ‘, always prints a space (whitespace character) in the sign position of positive values. If ‘-‘, omit the sign character of positive values. (default ‘-‘)
formatterdict of callables, optional 自定义设置数据类型的输出格式
If not None, the keys should indicate the type(s) that the respective formatting function applies to. Callables should return a string. Types that are not specified (by their corresponding keys) are handled by the default formatters. Individual types for which a formatter can be set are:
- ‘bool’
- ‘int’
- ‘timedelta’ : a
numpy.timedelta64
- ‘datetime’ : a
numpy.datetime64
- ‘float’
- ‘longfloat’ : 128-bit floats
- ‘complexfloat’
- ‘longcomplexfloat’ : composed of two 128-bit floats
- ‘numpystr’ : types
numpy.string_
andnumpy.unicode_
- ‘object’ : np.object_ arrays
- ‘str’ : all other strings
Other keys that can be used to set a group of types at once are:
- ‘all’ : sets all types
- ‘int_kind’ : sets ‘int’
- ‘float_kind’ : sets ‘float’ and ‘longfloat’
- ‘complex_kind’ : sets ‘complexfloat’ and ‘longcomplexfloat’
- ‘str_kind’ : sets ‘str’ and ‘numpystr’
floatmodestr, optional 控制浮点类型的precision选项的解释。精度取舍方式
Controls the interpretation of the precision option for floating-point types. Can take the following values (default maxprec_equal):
‘fixed’: Always print exactly precision fractional digits,
even if this would print more or fewer digits than necessary to specify the value uniquely.
‘unique’: Print the minimum number of fractional digits necessary
to represent each value uniquely. Different elements may have a different number of digits. The value of the precision option is ignored.
‘maxprec’: Print at most precision fractional digits, but if
an element can be uniquely represented with fewer digits only print it with that many.
‘maxprec_equal’: Print at most precision fractional digits,
but if every element in the array can be uniquely represented with an equal number of fewer digits, use that many digits for all elements.
legacystring or False, optional
If set to the string ‘1.13’ enables 1.13 legacy printing mode. This approximates numpy 1.13 print output by including a space in the sign position of floats and different behavior for 0d arrays. If set to False, disables legacy mode. Unrecognized strings will be ignored with a warning for forward compatibility.
New in version 1.14.0.
1. 打印全部
np.set_printoptions(threshold=np.inf)
示例:
1 | import numpy as np |
输出为:
[[0.50579515 0.40849974 0.58216481 … 0.06893892 0.26127332 0.68860937]
[0.26794823 0.2141258 0.21447686 … 0.39980584 0.45611762 0.79605551]
[0.84376421 0.80653402 0.45959844 … 0.608986 0.74416143 0.86017541]
…
[0.81536718 0.77777291 0.03696664 … 0.56042895 0.19054145 0.65038453]
[0.49530963 0.26861925 0.50227139 … 0.34600939 0.22184629 0.1005635 ]
[0.27232276 0.74490109 0.4380997 … 0.66812692 0.78472921 0.4887682 ]]
使用设置后可将中间省略号部分内容显示输出
1 | import numpy as np |
2. 精度
1 | np.set_printoptions(precision=4) |
1
2 > [1.1235]
>
3.输出格式
1 | np.set_printoptions(formatter={'all':lambda x: 'int: '+str(-x)}) |
array([int: 0, int: -1, int: -2])
1 | np.set_printoptions() # formatter gets reset |
array([0, 1, 2])
4.浮点表示
1 | with np.printoptions(precision=2, suppress=True, threshold=5): |
array([ 0. , 1.11, 2.22, …, 7.78, 8.89, 10. ])
5.打印时逗号分隔
1 | x = np.arange(3) |
array([0, 1, 2])