- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python numpy格式化打印的实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1.问题描述 。
在使用numpy的时候,我们经常在debug的时候将numpy数组打印下来,但是有的时候数组里面都是小数,数组又比较大,打印下来的时候非常不适合观察。这里主要讲一下如何让numpy打印的结果更加简洁 。
2.问题解决 。
这里需要使用numpy的set_printoptions函数,对应numpy源码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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的例子如下所示:
1
2
3
4
5
6
7
8
9
10
|
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之后,接下来所有的打印过程都会受到影响,然而有的时候我们并不希望如此,这个时候我们可以添加一个上下文管理器,只在规定的上下文环境当中设置我们需要的打印参数,其他地方仍然使用默认的打印参数,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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]
|
上面的程序中,我们通过使用contextlib里面的contextmanager为函数set_printoptions设置了上下文,在执行with里面的代码之前,设置打印的参数为precison=3,suppress=True,当跳出with代码块的时候,将打印参数设置为原来默认的打印参数.
这篇python numpy格式化打印的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/yaoqi_isee/article/details/77720792 。
最后此篇关于python numpy格式化打印的实例的文章就讲到这里了,如果你想了解更多关于python numpy格式化打印的实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
有没有更好的方法用 PHP 将数据输出到 html 页面? 如果我想在 php 中用一些 var 制作一个 div,我会写类似的东西 print (''.$var.''); 或 echo "''.$v
我可以使用 java awt print 来打印文档/文件而不是使用 javax print 吗?我发现在 java awt print 中有一个选项可以使用 AttributedString 将内容
目前我通过以下方式运行 R 脚本: R --slave argument1 argument2 ... 我想知道 R 中关于如何退出脚本并发出警告的最佳实践,q() 会这样做吗? if(!file.
谁能告诉我如何编写一个程序,用 gcc 编译时打印 c ,用 g++ 编译时打印 c++? 最佳答案 #ifdef __cplusplus printf("c++\n"); #else
我需要支持在 KitKat 设备上打印,但我的目标 SDK 是 13(无法更改)。 特别是我需要打印一个 webview。 这是用于打印 webview 的 API: http://developer
我正在尝试创建一个简单的函数,其中 python 将根据您的年份输入计算年龄。我已经尝试了几种方法,但我没有运气 atm。 附:对不起,我是新手。 ame = input(" Enter your n
JavaFX 2.0 是否支持打印?我有一个文本区域,我从中获取文本然后我想打印它,但似乎没有这个功能。 当然,这里我说的是打印到打印机。 :) 最佳答案 尚不支持。作为一种解决方法,您可以使用 Ja
我试图找出printOn的重点。我查看了一些实现它的类,看起来它只是帮助打印不同数据类型的单位。这是准确的吗? 如果是这样,有人能指出我如何为我自己的类(class)实现这一点的正确方向吗?我将在可能
我无法让 IE 打印我的 Canvas (使用 excanvas 生成)...我使用的是最新版本的 excanvas。 http://dl.dropbox.com/u/997831/canvas.ht
我搜索了很多但没有人回答我的问题,我读到在这样的信号处理程序中使用 cout 是不安全的: void ctrlZHandler(int sig_num) { //SIGTSTP-18
我有兴趣打印一系列查询。我有以下代码。 start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764) end = datetime.datetime(201
public class javaClass { public static void main(String [] arg) { String row1 = "A____A"
我需要写入前一行的命令,例如不带\n 的 print()。 下面是一些示例代码: a=0 print("Random string value") if a==0: print_to_prev
我有一个使用 UIKit 和 Objective C 的旧 iOS 应用程序,我目前正在将其移植到 SwiftUI 和 Swift。一切都很顺利,我喜欢 Swift 和 SwiftUI。该应用程序已经
我创建了一个求和函数,它接受一个开始编号和一个结束编号,并返回这两点之间的总和答案 def print_sum_equations(start_number,end_number):
在 Perl 6 中,print 和有什么区别? , put和 say ? 我怎么看 print 5不同,但 put 5和 say 5看起来一样。 最佳答案 put $a就像 print $a.Str
我正在使用 here 中的 getOrgChart 库,我正在尝试打印整个图表,而不仅仅是可见部分。不幸的是,当使用标准库打印功能时,它只会打印出第一部分,而我不知道如何打印整个图表(该图表相当宽,大
我制作了一个非常适合 A4 页面的 View 。现在我想打印它。请注意,我没有使用drawRect或类似的东西,只是一个带有 subview 和文本标签的普通 View 。我的问题是,我对该 View
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
这个问题已经有答案了: Printing a TDBGrid (4 个回答) 已关闭 6 年前。 如何在不安装或下载组件的情况下打印 DBGrid? 或者 如何将 DBGrid 的数据放入 RichE
我是一名优秀的程序员,十分优秀!