怎么在python中格式化打印numpy

发布时间:2021-04-30 16:52:01 作者:Leah
来源:亿速云 阅读:257

本篇文章给大家分享的是有关怎么在python中格式化打印numpy,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

1.问题描述

在使用numpy的时候,我们经常在debug的时候将numpy数组打印下来,但是有的时候数组里面都是小数,数组又比较大,打印下来的时候非常不适合观察。这里主要讲一下如何让numpy打印的结果更加简洁

2.问题解决

这里需要使用numpy的set_printoptions函数,对应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来控制小数点后面最多显示的位数

设置suppress来取消使用科学计数法

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之后,打印下来的结果简洁了很多,绝大多数时候我们只需要观察简洁的打印结果,太过精确的结果反而会因为占位太长不易于观察

2.2完整示例

2.1的例子中存在的一个问题是,一旦我们在程序的某一行设置了printoptions之后,接下来所有的打印过程都会受到影响,然而有的时候我们并不希望如此,这个时候我们可以添加一个上下文管理器,只在规定的上下文环境当中设置我们需要的打印参数,其他地方仍然使用默认的打印参数,代码如下:

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,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. 怎么在python中打印字符
  2. 怎么在python中打印菱形

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python numpy

上一篇:使用python怎么在字符后添加空格

下一篇:使用Python怎么检测网络延迟

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》