在没有科学记数法且给定精度的情况下漂亮地打印 NumPy 数组
如何以类似于此的方式打印格式化的 NumPy 数组:
x = 1.23456
print('%.3f' % x)
如果我想打印浮点数的 numpy.ndarray ,它会打印几个小数,通常以“科学”格式,这是相当困难的甚至可以读取低维数组。然而,numpy.ndarray
显然必须打印为字符串,即使用%s
。有解决办法吗?
How do I print formatted NumPy arrays in a way similar to this:
x = 1.23456
print('%.3f' % x)
If I want to print the numpy.ndarray
of floats, it prints several decimals, often in 'scientific' format, which is rather hard to read even for low-dimensional arrays. However, numpy.ndarray
apparently has to be printed as a string, i.e., with %s
. Is there a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
使用
numpy.set_printoptions
进行设置输出的精度:并且
suppress
禁止对小数字使用科学记数法:要在本地应用打印选项,使用 NumPy 1.15.0 或更高版本,您可以使用
numpy.printoptions
上下文管理器。例如,在
with-suite
内部设置了precision=3
和suppress=True
:但在
with-suite
外部code> 打印选项恢复为默认设置:如果您使用的是早期版本的 NumPy,您可以创建上下文管理器
你自己。例如,
为了防止从浮点数末尾去除零:
np.set_printoptions
现在有一个formatter
参数,它允许您指定一个每种类型的格式化函数。它打印
而不是
Use
numpy.set_printoptions
to set the precision of the output:And
suppress
suppresses the use of scientific notation for small numbers:To apply print options locally, using NumPy 1.15.0 or later, you could use the
numpy.printoptions
context manager.For example, inside the
with-suite
precision=3
andsuppress=True
are set:But outside the
with-suite
the print options are back to default settings:If you are using an earlier version of NumPy, you can create the context manager
yourself. For example,
To prevent zeros from being stripped from the end of floats:
np.set_printoptions
now has aformatter
parameter which allows you to specify a format function for each type.which prints
instead of
使用
np.array_str
来应用格式化为仅单个打印语句。它提供了 np.set_printoptions 功能的子集。例如:
Use
np.array_str
to apply formatting to only a single print statement. It gives a subset ofnp.set_printoptions
's functionality.For example:
Unutbu 给出了一个非常完整的答案(他们也从我这里得到了 +1),但这里有一个低技术替代方案:
作为一个函数(使用
format()
语法进行格式化)::
用法 数组的索引可以通过格式字符串访问:
Unutbu gave a really complete answer (they got a +1 from me too), but here is a lo-tech alternative:
As a function (using the
format()
syntax for formatting):Usage:
The index of the array is accessible in the format string:
仅供参考 Numpy 1.15(发布日期待定)将 包括一个上下文管理器,用于在本地设置打印选项。这意味着以下内容将与接受的答案(由unutbu和Neil G)中的相应示例相同,而无需编写自己的上下文管理器。例如,使用他们的例子:
FYI Numpy 1.15 (release date pending) will include a context manager for setting print options locally. This means that the following will work the same as the corresponding example in the accepted answer (by unutbu and Neil G) without having to write your own context manager. E.g., using their example:
使获取字符串结果变得非常容易(在今天的 numpy 版本中)的 gem 隐藏在 denis 答案中:
np.array2string
< /a>The gem that makes it all too easy to obtain the result as a string (in today's numpy versions) is hidden in denis answer:
np.array2string
多年后,下面是另一幅。但对于日常使用我只是
Years later, another one is below. But for everyday use I just
这是我使用的,而且非常简单:
And here is what I use, and it's pretty uncomplicated:
可以使用
round
方法如 x.round(decimals) 所示,它返回一个新的 numpy 数组,其中元素相应地舍入。One can use
round
method as inx.round(decimals)
which returns a new numpy array with elements rounded accordingly.很惊讶没有看到提到的
around
方法 - 意味着不会弄乱打印选项。Was surprised to not see
around
method mentioned - means no messing with print options.我用
多维数组修改起来并不困难。
I use
It's not difficult to modify it for multi-dimensional arrays.
我经常希望不同的列具有不同的格式。以下是我如何通过将 NumPy 数组(的切片)转换为元组,使用某种格式来打印简单的 2D 数组:
I often want different columns to have different formats. Here is how I print a simple 2D array using some variety in the formatting by converting (slices of) my NumPy array to a tuple:
我发现当使用循环显示列表或数组时,通常的浮点格式 {:9.5f} 可以正常工作——抑制小值电子符号。但是,当格式化程序在单个打印语句中包含多个项目时,该格式有时无法抑制其电子符号。例如:
我的结果显示了情况 4、5 和 6 中的错误:
我对此没有任何解释,因此我总是使用循环来浮动输出多个值。
I find that the usual float format {:9.5f} works properly -- suppressing small-value e-notations -- when displaying a list or an array using a loop. But that format sometimes fails to suppress its e-notation when a formatter has several items in a single print statement. For example:
My results show the bug in cases 4, 5, and 6:
I have no explanation for this, and therefore I always use a loop for floating output of multiple values.
numpy.char.mod
也可能有用,具体取决于您的应用程序的详细信息,例如:numpy.char.mod('Value=%4.2f', numpy.arange(5, 10, 0.1))
将返回一个字符串数组,其中包含元素“Value=5.00”、“Value=5.10”等(作为一个有点人为的示例)。numpy.char.mod
may also be useful, depending on the details of your application e.g.:numpy.char.mod('Value=%4.2f', numpy.arange(5, 10, 0.1))
will return a string array with elements "Value=5.00", "Value=5.10" etc. (as a somewhat contrived example).另一种选择是使用十进制模块:
Yet another option is to use the
decimal
module: