16 Python Numpy 中的打印设置set_printoptions( )函数

numpy.set_printoptions()函数
set_printoptions(precision=None, threshold=None, edgeitems=None, 
				 linewidth=None,suppress=None, nanstr=None, 
				 infstr=None, formatter=None, sign=None,
				 floatmode=None, **kwarg)

precision 浮点数组输出的精度位数,即小数点后位数(默认为8)。

## precision:控制陣列內容微幅點數時的列印精度(小數位數)
np.set_printoptions(precision=1)
print("NumPy set_printoptions(precision=1)\n", np.linspace(3, 5, 10))
print()
np.set_printoptions(precision=3)
print("NumPy set_printoptions(precision=3)\n", np.linspace(3, 5, 10))



Output:
NumPy set_printoptions(precision=1)
[3. 3.2 3.4 3.7 3.9 4.1 4.3 4.6 4.8 5. ]
NumPy set_printoptions(precision=3)
[3. 3.222 3.444 3.667 3.889 4.111 4.333 4.556 4.778 5. ]

threshold 元素门槛值。数组个数沒有超过设置的阈值,NumPy就会将Python將所有元素列印出來。

##threshold
# 當要列印的陣列太大時,NumPy會自動以...代替一部分的內容,以減少篇幅!
# 但是可以透過全域設定set_printoptions設定threshold(門檻值),
# 元素數量少於或等於門檻值的陣列就會全部列印出來,
# 相反地,元素數量高於門檻值,列印時就會省略部分內容。
# 陣列大小:20,門檻值20 => 陣列元素數量小於等於門檻值,應列印全部內容
np.set_printoptions(threshold=20)
print("NumPy set_printoptions(threshold=20)\n", np.arange(20))
print()
# 陣列大小:20,門檻值15 => 陣列元素數量大於門檻值,應省略部分內容
np.set_printoptions(threshold=15)
print("NumPy set_printoptions(threshold=15)\n", np.arange(20))

Output:
NumPy set_printoptions(threshold=20)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
NumPy set_printoptions(threshold=15)
[ 0 1 2 3 4 ... 15 16 17 18 19]



lmsdem = gdal_array.LoadFile(path)
region = np.s_[1450:1500,1350:1400]
z = lmsdem[region]
np.set_printoptions(threshold=1000000) # print的元素个数小于1000000个,全部显示

print("="*30)

Output:
print(z)  # 当threshold=1000000 时,实际打印的元素个数小于1000000,则打印结全部结果
[[1678 1718 1690 1660 1632 1641 1588 1491 1438 1411]
 [1670 1630 1580 1560 1530 1550 1541 1490 1413 1362]
 [1576 1559 1489 1461 1428 1435 1458 1426 1366 1323]
 [1482 1500 1422 1375 1342 1324 1353 1355 1314 1274]
 [1471 1457 1380 1304 1275 1257 1249 1278 1261 1250]
 [1379 1370 1312 1266 1265 1259 1246 1233 1243 1238]
 [1331 1298 1295 1338 1361 1336 1303 1251 1241 1237]
 [1363 1335 1372 1428 1458 1410 1363 1311 1270 1258]
 [1407 1381 1420 1485 1515 1485 1432 1367 1321 1321]
 [1427 1461 1476 1531 1574 1537 1476 1435 1395 1382]]
==============================
print(z) # 当threshold=100 时,实际打印的元素个数大于100,则打印结果部分省略了
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

edgeitems 当省略数组内元素内容时要显示的元素数量。

## edgeitems:當列印陣列時需要省略內容,edgeitems會決定要印出來的元素有幾個
np.set_printoptions(threshold=10, edgeitems=3)
print("NumPy set_printoptions(edgeitems=3)\n", np.arange(15))
print()
np.set_printoptions(threshold=10, edgeitems=5)
print("NumPy set_printoptions(edgeitems=5)\n", np.arange(15))


Output:
NumPy set_printoptions(edgeitems=3)
[ 0 1 2 ... 12 13 14]
NumPy set_printoptions(edgeitems=5)
[ 0 1 2 3 4 ... 10 11 12 13 14]

linewidth 每一行要打印的元素个数

## linewidth
np.set_printoptions(linewidth=10)
print("NumPy set_printoptions(linewidth=10)\n", np.arange(15))
print()
np.set_printoptions(linewidth=20)
print("NumPy set_printoptions(linewidth=25)\n", np.arange(15))

Output:
NumPy set_printoptions(linewidth=10)
[ 0 1 2
3 4 5
6 7 8
9 10 11
12 13 14]
NumPy set_printoptions(linewidth=25)
[ 0 1 2 3 4 5
6 7 8 9 10 11
12 13 14]

suppress 是否要打印显示小数位

# suppress
np.set_printoptions(suppress=True)
print("NumPy set_printoptions(suppress=True)\n", np.arange(0, 1, 0.00
print()
np.set_printoptions(suppress=False)
print("NumPy set_printoptions(suppress=False)\n", np.arange(0, 1, 0.0


Output:
NumPy set_printoptions(suppress=True)
[0. 0. 0. ... 1. 1. 1.]
NumPy set_printoptions(suppress=False)
[0.e+00 1.e-05 2.e-05 ... 1.e+00 1.e+00 1.e+00]

nanstr 当数组元素值出现NaN时所要显示的字符串

## nanstr
# 當陣列元素值出現not-a-number時要顯示的內容為何
np.set_printoptions(nanstr="Oops!")
a = np.array([np.nan, 1, np.nan], dtype=np.float16)
print("NumPy set_printoptions(nanstr=\"Oops!\")\n", a)


Output:
NumPy set_printoptions(nanstr="Oops!")
[Oops! 1. Oops!]

infstr 数组元素值出現inf时所显示的字串

## infstr
# 當陣列元素值出現inf(無限大)時要顯示的內容為何
np.set_printoptions(infstr="∞")
a = np.array([np.inf, 8, -np.inf])
print("NumPy set_printoptions(infstr=\"∞\")\n", a)

Output:
NumPy set_printoptions(infstr="∞")
[8. -]

formatter 使用lambda函數客製列印陣列元素的格式

## formatter
# 使用lambda函數客製列印陣列元素的格式
np.set_printoptions() # reset print options
np.set_printoptions(formatter={'all': lambda x: 'i:' + str(x)})
print("NumPy set_printoptions(formatter={all: i:})\n", np.arange(0,5)
print()
# 這邊可以看出來,只設定了float的格式,是不會對int產生影響的
np.set_printoptions(formatter={'float_kind': lambda x: 'f:' + str(x)
print("NumPy set_printoptions(formatter={float_kind: f:})\n", np.arange(0,5))
print("NumPy set_printoptions(formatter={float_kind: f:})\n", np.arange(0,5))
print()
# 如果同時只想要客製float和int的格式,可以用逗號隔開設定值
np.set_printoptions(formatter={'int_kind': lambda x: 'i:' + str(x),
print("NumPy set_printoptions({int_kind: i:, float_kind: f:})\n", np.arange(0,5))
print("NumPy set_printoptions({int_kind: i:, float_kind: f:})\n", np.arange(0,5))



Output:
NumPy set_printoptions(formatter={all: i:})
[i:0 i:1 i:2 i:3 i:4]
NumPy set_printoptions(formatter={float_kind: f:})
[0 1 2 3 4]
NumPy set_printoptions(formatter={float_kind: f:})
[f:0.0 f:1.0 f:2.0 f:3.0 f:4.0]
NumPy set_printoptions({int_kind: i:, float_kind: f:})
[i:0 i:1 i:2 i:3 i:4]
NumPy set_printoptions({int_kind: i:, float_kind: f:})
[f:0.0 f:1.0 f:2.0 f:3.0 f:4.0]

sign 控制正负号

## sign
# 當sign='+'時,就會連正數都會加上正號
np.set_printoptions(sign='+')
a = np.array([9, 8, -6, -7], dtype=np.float16)
print("NumPy set_printoptions(sign=\"+\")\n", a)
print()
# 當sign='-'時(預設值),就是只有數值<0時,才會加上負號
np.set_printoptions(sign='-')
a = np.array([9, 8, -6, -7], dtype=np.float16)
print("NumPy set_printoptions(sign=\"-\")\n", a)

Output:
NumPy set_printoptions(sign="+")
[+9. +8. -6. -7.]
NumPy set_printoptions(sign="-")
[ 9. 8. -6. -7.]

floatmode 控制“精度”选项的解释浮点类型。

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值