python numpy格式化打印的實(shí)例
1.問(wèn)題描述
在使用numpy的時(shí)候,我們經(jīng)常在debug的時(shí)候?qū)umpy數(shù)組打印下來(lái),但是有的時(shí)候數(shù)組里面都是小數(shù),數(shù)組又比較大,打印下來(lái)的時(shí)候非常不適合觀察。這里主要講一下如何讓numpy打印的結(jié)果更加簡(jiǎn)潔
2.問(wèn)題解決
這里需要使用numpy的set_printoptions函數(shù),對(duì)應(yīng)numpy源碼如下所示:
def set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None, suppress=None,
nanstr=None, infstr=None,
formatter=None):
"""
Set printing options.
These options determine the way floating point numbers, arrays and
other NumPy objects are displayed.
Parameters
----------
precision : int, optional
Number of digits of precision for floating point output (default 8).
threshold : int, optional
Total number of array elements which trigger summarization
rather than full repr (default 1000).
edgeitems : int, optional
Number of array items in summary at beginning and end of
each dimension (default 3).
linewidth : int, optional
The number of characters per line for the purpose of inserting
line breaks (default 75).
suppress : bool, optional
Whether or not suppress printing of small floating point values
using scientific notation (default False).
nanstr : str, optional
String representation of floating point not-a-number (default nan).
infstr : str, optional
String representation of floating point infinity (default inf).
formatter : dict of callables, optional
這里我們主要用到其中的兩個(gè)屬性:
設(shè)置precision來(lái)控制小數(shù)點(diǎn)后面最多顯示的位數(shù)
設(shè)置suppress來(lái)取消使用科學(xué)計(jì)數(shù)法
2.1 簡(jiǎn)單示例
一個(gè)簡(jiǎn)單的利用set_printoptions的例子如下所示:
import numpy as np
a = np.random.random(3)
print('before set options: \n {}'.format(a))
np.set_printoptions(precision=3, suppress=True)
print('after set options: \n {}'.format(a))
>>>
before set options:
[ 0.05856348 0.5417039 0.76520603]
after set options:
[ 0.059 0.542 0.765]
可以看到,設(shè)置了打印的options之后,打印下來(lái)的結(jié)果簡(jiǎn)潔了很多,絕大多數(shù)時(shí)候我們只需要觀察簡(jiǎn)潔的打印結(jié)果,太過(guò)精確的結(jié)果反而會(huì)因?yàn)檎嘉惶L(zhǎng)不易于觀察
2.2完整示例
2.1的例子中存在的一個(gè)問(wèn)題是,一旦我們?cè)诔绦虻哪骋恍性O(shè)置了printoptions之后,接下來(lái)所有的打印過(guò)程都會(huì)受到影響,然而有的時(shí)候我們并不希望如此,這個(gè)時(shí)候我們可以添加一個(gè)上下文管理器,只在規(guī)定的上下文環(huán)境當(dāng)中設(shè)置我們需要的打印參數(shù),其他地方仍然使用默認(rèn)的打印參數(shù),代碼如下:
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('-----------before set options-----------')
print('x = {}'.format(x))
print('y = {}'.format(y))
with printoptions(precision=3, suppress=True):
print('------------set options------------')
print('x = {}'.format(x))
print('y = {}'.format(y))
print('---------------set back options-------------')
print('x = {}'.format(x))
print('y = {}'.format(y))
>>>
-----------before set options-----------
x = [ 0.3802371 0.7929781 0.14008782]
y = [ 1.50000000e-02 1.50000000e+00 1.50000000e+03]
------------set options------------
x = [ 0.38 0.793 0.14 ]
y = [ 0.015 1.5 1500. ]
---------------set back options-------------
x = [ 0.3802371 0.7929781 0.14008782]
y = [ 1.50000000e-02 1.50000000e+00 1.50000000e+03]
上面的程序中,我們通過(guò)使用contextlib里面的contextmanager為函數(shù)set_printoptions設(shè)置了上下文,在執(zhí)行with里面的代碼之前,設(shè)置打印的參數(shù)為precison=3,suppress=True,當(dāng)跳出with代碼塊的時(shí)候,將打印參數(shù)設(shè)置為原來(lái)默認(rèn)的打印參數(shù)。
這篇python numpy格式化打印的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python dataclass 快速創(chuàng)建數(shù)據(jù)類(lèi)的方法
在Python中,dataclass是一種用于快速創(chuàng)建數(shù)據(jù)類(lèi)的裝飾器和工具,本文實(shí)例代碼中我們定義了一個(gè)Person數(shù)據(jù)類(lèi),并使用fields()函數(shù)遍歷其字段,打印出每個(gè)字段的名稱(chēng)、類(lèi)型、默認(rèn)值和元數(shù)據(jù),對(duì)python dataclass 數(shù)據(jù)類(lèi)相關(guān)知識(shí)感興趣的朋友一起看看吧2024-03-03
Python字符串編碼轉(zhuǎn)換 encode()和decode()方法詳細(xì)說(shuō)明
這篇文章主要介紹了Python字符串編碼轉(zhuǎn)換 encode()和decode()方法詳細(xì)的說(shuō)明,下面文章圍繞encode()和decode()方法的相相關(guān)資料展開(kāi)內(nèi)容,具有一定的價(jià)值,需要的朋友卡通參考一下2021-12-12
Python用戶(hù)自定義異常的實(shí)現(xiàn)
這篇文章主要介紹了Python用戶(hù)自定義異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
基于Python和Tkinter實(shí)現(xiàn)高考倒計(jì)時(shí)功能
隨著高考的臨近,每個(gè)考生都在緊鑼密鼓地復(fù)習(xí),這時(shí)候,一款實(shí)用的倒計(jì)時(shí)軟件能有效幫助你規(guī)劃剩余時(shí)間,提醒你不要浪費(fèi)每一分每一秒,今天,我們來(lái)聊聊一款基于Python和Tkinter開(kāi)發(fā)的高考倒計(jì)時(shí)軟件,功能簡(jiǎn)單卻極具實(shí)用性,讓你在緊張的備考過(guò)程中不再迷失2025-03-03
Python內(nèi)置函數(shù)all()的實(shí)現(xiàn)
Python內(nèi)置函數(shù)?all()?用于判斷可迭代對(duì)象中的所有元素是否都為真值(Truthy),是邏輯判斷的重要工具,下面就來(lái)具體介紹如何使用,感興趣的可以了解一下2025-04-04

