Numpy 的 set_printoptions(~) 方法自定义 Numpy 数组的打印方式。
参数
1.precision | int 或 None | optional
要显示的小数位数。精度为 3 意味着 3.1415 变为 3.142 。如果传递 None 并且 floatmode 不是 fixed ,则精度将使得它们的值唯一区分。默认情况下,precision=8 。
2. threshold | int | optional
如果数组中的值数量大于 threshold ,则不会打印每个值,而是使用 ... 截断这些值。默认情况下,threshold=1000 。
3. edgeitems | int | optional
如果发生截断,则显示在前面和后面的值的数量。默认情况下,edgeitems=3 。
4. linewidth | int | optional
每行的字符数。默认情况下,linewidth=75 。
5. suppress | boolean | optional
是否以完整小数显示值而不是使用科学记数法。仅适用于绝对值小于 1e-4 或数组中最大值与最小值之比大于 1000 的浮点数。默认为 suppress=False 。
6. nanstr | string | optional
用于替换数组中任何 nan 的字符串。默认情况下,nanstr="nan" .
7. infstr | string | optional
用于替换数组中任何 inf 的字符串。默认情况下,infstr="inf" 。
8. sign | string | optional
如何处理值的符号:
|
值 |
说明 |
|---|---|
|
"-" |
省略+。 |
|
"+" |
在正数前面放置一个 +。 |
|
" " |
在正数前面放置一个空格。 |
默认情况下,sign="-" 。
9. formatter | dict<string,function> | optional
适用于不同数据类型的映射。字典的键值对如下:
-
key:您希望应用映射的类型
-
value:一个函数,它将类型为
key的值作为输入,并返回一个新值。
以下是一些主要数据类型:
|
类型 |
说明 |
|---|---|
|
"bool" |
转换布尔值。 |
|
"int" |
转换整数。 |
|
"float" |
转换浮点数。 |
|
"timedelta" |
转换时间增量。 |
|
"datatime" |
转换日期时间。 |
以下是您可以提供的一些特殊 key :
|
钥匙 |
说明 |
|---|---|
|
"all" |
转换所有数据类型。 |
|
"float_kind" |
转换"float"和"longfloat" |
|
"str_kind" |
转换"str"和"numpystr" |
默认情况下,formatter=None 。
10.floatmode | string | optional
如何处理浮点数的精度:
|
值 |
说明 |
|---|---|
|
"fixed" |
始终显示指定的 |
|
"unique" |
显示最小小数位数,以便唯一标识值。这会忽略指定的 |
|
"maxprec" |
最多打印指定的 |
|
"maxprec_equal" |
最多打印指定的 |
默认情况下,floatmode="maxprec_equal" 。
返回值
没有,因为此方法只是打印在屏幕上。
例子
基本用法
显示 3 个小数位:
np.set_printoptions(precision=3)
np.array([0.000005, 3.1416])
array([5.000e-06, 3.142e+00])
要获得未修复的 precision ,请传递 None :
np.set_printoptions(precision=None)
np.array([3.14, 3.1416])
array([3.14 , 3.142])
指定阈值
默认情况下, threshold=1000 ,这意味着将汇总具有 1000 个或更多值的打印数组:
print(np.arange(1500))
[ 0 1 2 ... 1497 1498 1499]
这意味着小数组不会被汇总:
print(np.arange(7))
[0 1 2 3 4 5 6]
我们可以设置一个阈值,这样即使是这些小数组也会被汇总:
np.set_printoptions(threshold=3)
np.arange(7)
array([0, 1, 2, ..., 4, 5, 6])
指定边项目
默认情况下, edgeitems=3 ,这意味着当汇总值时,左侧将显示 3 个值,右侧将显示 3 个值:
print(np.arange(1500))
[ 0 1 2 ... 1497 1498 1499]
我们可以通过设置我们自己的 edgeitems 来自定义它:
np.set_printoptions(edgeitems=4)
print(np.arange(1500))
[ 0 1 2 3 ... 1496 1497 1498 1499]
指定线宽
默认情况下, linewidth=75 ,这意味着每打印行最多可以有 75 个字符:
print(np.array([12, 34, 5]))
[12 34 5]
每行最多只打印 7 个字符:
np.set_printoptions(linewidth=7)
print(np.array([12, 34, 5]))
[12 34
5]
指定抑制
要显示小于 1e-4 的数字的所有小数位:
np.set_printoptions(suppress=True)
print(np.array([1e-5]))
[0.00001]
suppress=False 的默认行为如下:
np.set_printoptions(suppress=False)
print(np.array([1e-5]))
[1.e-05]
指定nanstr
np.set_printoptions(nanstr="Missing!")
print(np.array([np.NaN, 3.14]))
[Missing! 3.14]
指定 infstr
np.set_printoptions(infstr="Infinity!")
print(np.array([np.inf, 3.14]))
[Infinity! 3.14]
指定标志
要显示正数的 + 符号:
np.set_printoptions(sign="+")
print(np.array([np.inf, 3.14, -2]))
[ +inf +3.14 -2. ]
要在正数前面添加" ":
np.set_printoptions(sign=" ")
print(np.array([np.inf, 3.14, -2]))
[ inf 3.14 -2. ]
这里很难看到,但添加了一个空格。
指定格式化程序
将所有布尔值 True 转换为 1 并将 False 转换为 "-1" 。
mapping = {
"bool": lambda x: "1" if x else "-1"
}
np.set_printoptions(formatter=mapping)
print(np.array([True, False, True]))
[1 -1 1]
这里,请确保您在映射中返回一个字符串,否则将引发错误。
指定浮点数模式
固定的
要打印具有相同小数位的浮点数(即默认为 8):
np.set_printoptions(floatmode="fixed")
print(np.array([5.05, 5.05001]))
[5.05000000 5.05001000]
独特
要打印具有最少小数位数的浮点数,以便唯一标识这些值:
np.set_printoptions(floatmode="unique")
print(np.array([5.05, 5.05001]))
[5.05 5.05001]
请注意,这会忽略 precision 参数。
最大预测值
与 unique 相同,但浮点数最多可以有 precision :
np.set_printoptions(floatmode="maxprec", precision=4)
print(np.array([5.05, 5.052999]))
[5.05 5.053]
maxprec_equal
与 maxprec 相同,但浮点数都具有相同的 precision :
np.set_printoptions(floatmode="maxprec_equal")
print(np.array([5.05, 5.05001]))
[5.05000 5.05001]
相关用法
- Python NumPy set_string_function方法用法及代码示例
- Python Django set用法及代码示例
- Python Django set_language用法及代码示例
- Python dict setdefault()用法及代码示例
- Python calendar setfirstweekday()用法及代码示例
- Python set clear()用法及代码示例
- Python NumPy setdiff1d方法用法及代码示例
- Python set构造函数用法及代码示例
- Python OpenCV setWindowTitle()用法及代码示例
- Python set()用法及代码示例
- Python setattr()用法及代码示例
- Python set copy()用法及代码示例
- Python set add()用法及代码示例
- Python set symmetric_difference_update()用法及代码示例
- Python OpenCV setTrackbarPos()用法及代码示例
- Python seaborn.swarmplot()用法及代码示例
- Python NumPy searchsorted方法用法及代码示例
- Python seaborn.residplot()用法及代码示例
- Python Django serve用法及代码示例
- Python seaborn.regplot()用法及代码示例
- Python Django sensitive_variables用法及代码示例
- Python BeautifulSoup select_one方法用法及代码示例
- Python seaborn.PairGrid()用法及代码示例
- Python Tableau server_info.get用法及代码示例
- Python BeautifulSoup select方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 NumPy | set_printoptions method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
