介绍
本篇文章给大家分享的是有关怎么在python中格式化打印numpy,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
python主要用来做什么
python主要应用于:1,网络开发;2、数据科学研究;3,网络爬虫;4、嵌入式应用开发,5日游戏开发;6桌面应用开发。
<强> 1。问题描述强>
在使用numpy的时候,我们经常在调试的时候将numpy数组打印下来,但是有的时候数组里面都是小的数,数组又比较大,打印下来的时候非常不适合观察。这里主要讲一下如何让numpy打印的结果更加简洁
<强> 2。问题解决强>
这里需要使用numpy的set_printoptions函数,对应numpy源码如下所示:
def set_printoptions(精度=没有,阈值=没有,edgeitems=没有 ,,,,,线宽=没有,抑制=没有 ,,,,,nanstr=没有,infstr=没有 ,,,,,格式化程序=None): ,“““ ,Set printing 选项。 ,These options  determine 从而way floating point 数字,arrays ,other NumPy  objects ,断开连接,显示。 ,参数 - - - - - - - - - - - ,precision : int,可选 Number 才能of digits  of precision for floating point output (default 8)。 ,threshold : int,可选 Total 才能number of  array elements which trigger 总结 rather 才能than full  repr (default 1000)。 ,edgeitems : int,可选 Number 才能of array  items 拷贝summary at beginning 以及最终获得的信息 each 才能;dimension (default 3)。 ,linewidth : int,可选 从而才能number of  characters per line for 从而purpose of 插入 line 才能;breaks (default 75)。 ,suppress : bool,可选 not  Whether 才能,或是;suppress printing of small floating point 价值 using 才能;scientific  notation (default 假)。 ,nanstr : str,可选 String 才能representation of  floating point not-a-number (default 南)。 ,infstr : str,可选 String 才能representation of  floating point infinity (default 正)。 ,formatter : dict of 可调用的,,可选
这里我们主要用到其中的两个属性:
设置精度来控制小数点后面最多显示的位数
设置抑制来取消使用科学计数法
<强> 2.1简单示例强>
一个简单的利用set_printoptions的例子如下所示:
import numpy as np 时间=a np.random.random (3) 打印(& # 39;before set 选择:,\ n {} & # 39; .format (a)) np.set_printoptions(精度=3,抑制=True) 打印(& # 39;after set 选择:,\ n {} & # 39; .format (a)) 在在在 before set 选择:, ,(0.05856348,0.5417039,0.76520603) after set 选择:, ,(0.059,0.542,0.765)
可以看的到,设置了打印的选项之后,打印下来的结果简洁了很多,绝大多数时候我们只需要观察简洁的打印结果,太过精确的结果反而会因为占位太长不易于观察
<强> 2.2完整示例强>
2.1的例子中存在的一个问题是,一旦我们在程序的某一行设置了printoptions之后,接下来所有的打印过程都会受到影响,然而有的时候我们并不希望如此,这个时候我们可以添加一个上下文管理器,只在规定的上下文环境当中设置我们需要的打印参数,其他地方仍然使用默认的打印参数,代码如下:
import numpy as np 得到contextlib import  contextmanager @contextmanager def printoptions (* args,, * * kwargs):=,,original_options  np.get_printoptions () ,np.set_printoptions (* args,, * * kwargs) ,试一试: ,产量 ,最后: np.set_printoptions才能(* * original_options) 时间=x np.random.random (3) 时间=y np.array([依照1.5,,1.5,,1500]) 打印(& # 39;- - - - - - - - - - - -before set 选项- - - - - - - - - - - - - - - - - # 39;) 打印(& # 39;x =, {} & # 39; .format (x)) 打印(& # 39;y =, {} & # 39; .format (y)) with printoptions(=3,精度,抑制=True): ,打印(& # 39;- - - - - - - - - - - - set 选项- - - - - - - - - - - - & # 39;) ,打印(& # 39;x =, {} & # 39; .format (x)) ,打印(& # 39;y =, {} & # 39; .format (y)) 打印(& # 39;- - - - - - - - - - - - - -set back 选项- - - - - - - - - - - - - # 39;) 打印(& # 39;x =, {} & # 39; .format (x)) 打印(& # 39;y =, {} & # 39; .format (y)) 在在在 - - - - - - - - - - - -before set 选项- - - - - - - - - - - - x =, (0.7929781, 0.14008782, 0.3802371) 时间=y [1.50000000, 1.50000000 e-02 e + 00, 1.50000000 e + 03] - - - - - - - - - - - - set 选项- - - - - - - - - - - - x =, (0.793, 0.14, 0.38,) y ,=,, 0.015, 1.5, 1500只) - - - - - - - - - - - - - -set back 选项- - - - - - - - - - - - - x =, (0.7929781, 0.14008782, 0.3802371) null怎么在python中格式化打印numpy