怎么在python中格式化打印numpy-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)怎么在python中格式化打印numpy,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

成都創(chuàng)新互聯(lián)專注于大同企業(yè)網(wǎng)站建設,響應式網(wǎng)站開發(fā),成都商城網(wǎng)站開發(fā)。大同網(wǎng)站建設公司,為大同等地區(qū)提供建站服務。全流程定制開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務Python主要用來做什么

Python主要應用于:1、Web開發(fā);2、數(shù)據(jù)科學研究;3、網(wǎng)絡爬蟲;4、嵌入式應用開發(fā);5、游戲開發(fā);6、桌面應用開發(fā)。

1.問題描述

在使用numpy的時候,我們經(jīng)常在debug的時候?qū)umpy數(shù)組打印下來,但是有的時候數(shù)組里面都是小數(shù),數(shù)組又比較大,打印下來的時候非常不適合觀察。這里主要講一下如何讓numpy打印的結(jié)果更加簡潔

2.問題解決

這里需要使用numpy的set_printoptions函數(shù),對應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

這里我們主要用到其中的兩個屬性:

設置precision來控制小數(shù)點后面最多顯示的位數(shù)

設置suppress來取消使用科學計數(shù)法

2.1 簡單示例

一個簡單的利用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]


可以看到,設置了打印的options之后,打印下來的結(jié)果簡潔了很多,絕大多數(shù)時候我們只需要觀察簡潔的打印結(jié)果,太過精確的結(jié)果反而會因為占位太長不易于觀察

2.2完整示例

2.1的例子中存在的一個問題是,一旦我們在程序的某一行設置了printoptions之后,接下來所有的打印過程都會受到影響,然而有的時候我們并不希望如此,這個時候我們可以添加一個上下文管理器,只在規(guī)定的上下文環(huán)境當中設置我們需要的打印參數(shù),其他地方仍然使用默認的打印參數(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]

以上就是怎么在python中格式化打印numpy,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:怎么在python中格式化打印numpy-創(chuàng)新互聯(lián)
標題路徑:http://muchs.cn/article34/dhehse.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站內(nèi)鏈關(guān)鍵詞優(yōu)化、自適應網(wǎng)站、營銷型網(wǎng)站建設、品牌網(wǎng)站設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司