kam*_*ame 512 python arrays numpy options output-formatting
当我打印一个numpy数组时,我得到一个截断的表示,但我想要完整的数组.
有没有办法做到这一点?
例子:
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[ 0, 1, 2, ..., 37, 38, 39],
[ 40, 41, 42, ..., 77, 78, 79],
[ 80, 81, 82, ..., 117, 118, 119],
...,
[9880, 9881, 9882, ..., 9917, 9918, 9919],
[9920, 9921, 9922, ..., 9957, 9958, 9959],
[9960, 9961, 9962, ..., 9997, 9998, 9999]])
Run Code Online (Sandbox Code Playgroud)
Raj*_*raj 536
import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
Run Code Online (Sandbox Code Playgroud)
Pau*_*Mag 203
import numpy as np
np.set_printoptions(threshold=np.inf)
Run Code Online (Sandbox Code Playgroud)
我建议使用np.inf而不是np.nan其他人建议使用.它们都可以用于您的目的,但是通过将阈值设置为"无限",每个人都可以阅读您的代码,这是显而易见的.有一个"不是数字"的门槛对我来说似乎有点模糊.
Ano*_*oyz 72
之前的答案是正确的,但作为一个较弱的替代方案,您可以转换为列表:
>>> numpy.arange(100).reshape(25,4).tolist()
[[0, 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, 33, 34, 35], [36, 37, 38, 39], [40, 41,
42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61,
62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73, 74, 75], [76, 77, 78, 79], [80, 81,
82, 83], [84, 85, 86, 87], [88, 89, 90, 91], [92, 93, 94, 95], [96, 97, 98, 99]]
Run Code Online (Sandbox Code Playgroud)
Ree*_*sey 41
这听起来像你正在使用numpy.
如果是这种情况,您可以添加:
import numpy as np
np.set_printoptions(threshold=np.nan)
Run Code Online (Sandbox Code Playgroud)
这将禁用角落打印.有关更多信息,请参阅此NumPy教程.
ger*_*rit 34
如果您使用NumPy 1.15(2018-07-23发布)或更新版本,则可以使用printoptions上下文管理器:
with numpy.printoptions(threshold=numpy.inf):
print(arr)
Run Code Online (Sandbox Code Playgroud)
(当然,取代numpy由np如果这就是你如何进口numpy)
使用上下文管理器(with-block)可确保在上下文管理器完成后,打印选项将恢复为块启动之前的任何内容.它确保设置是临时的,并且仅应用于块内的代码.
有关上下文管理器的详细信息以及它支持的其他参数,请参阅numpy.printoptions文档.
ZSG*_*ZSG 32
这是一种一次性的方法,如果您不想更改默认设置,这将非常有用:
def fullprint(*args, **kwargs):
from pprint import pprint
import numpy
opt = numpy.get_printoptions()
numpy.set_printoptions(threshold='nan')
pprint(*args, **kwargs)
numpy.set_printoptions(**opt)
Run Code Online (Sandbox Code Playgroud)
neo*_*eok 28
使用上下文管理作为保价 sugggested
import numpy as np
class fullprint:
'context manager for printing full numpy arrays'
def __init__(self, **kwargs):
kwargs.setdefault('threshold', np.inf)
self.opt = kwargs
def __enter__(self):
self._opt = np.get_printoptions()
np.set_printoptions(**self.opt)
def __exit__(self, type, value, traceback):
np.set_printoptions(**self._opt)
a = np.arange(1001)
with fullprint():
print(a)
print(a)
with fullprint(threshold=None, edgeitems=10):
print(a)
Run Code Online (Sandbox Code Playgroud)
Cir*_*四事件 12
numpy.savetxt
numpy.savetxt(sys.stdout, numpy.arange(10000))
Run Code Online (Sandbox Code Playgroud)
或者如果你需要一个字符串:
import StringIO
sio = StringIO.StringIO()
numpy.savetxt(sio, numpy.arange(10000))
s = sio.getvalue()
print s
Run Code Online (Sandbox Code Playgroud)
默认输出格式为:
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
...
Run Code Online (Sandbox Code Playgroud)
并且可以使用其他参数进行配置.
在Python 2.7.12上测试,numpy 1.11.1.
MSe*_*ert 10
这是一个微小的修饰(除去传递额外的参数选项set_printoptions)的neok的回答.
它展示了如何使用contextlib.contextmanager更少的代码行轻松创建这样的上下文管理器:
import numpy as np
from contextlib import contextmanager
@contextmanager
def show_complete_array():
oldoptions = np.get_printoptions()
np.set_printoptions(threshold=np.inf)
try:
yield
finally:
np.set_printoptions(**oldoptions)
Run Code Online (Sandbox Code Playgroud)
在您的代码中,它可以像这样使用:
a = np.arange(1001)
print(a) # shows the truncated array
with show_complete_array():
print(a) # shows the complete array
print(a) # shows the truncated array (again)
Run Code Online (Sandbox Code Playgroud)
除了最大列数(以固定)之外,此答案numpy.set_printoptions(threshold=numpy.nan)还可以限制显示的字符数。在某些环境中,例如从bash调用python(而不是交互式会话)时,可以通过如下设置参数来解决此问题linewidth。
import numpy as np
np.set_printoptions(linewidth=2000) # default = 75
Mat = np.arange(20000,20150).reshape(2,75) # 150 elements (75 columns)
print(Mat)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您的窗口应限制换行符的字符数。
对于那些使用sublime文本并希望在输出窗口中查看结果的用户,应将build选项添加"word_wrap": false到sublime-build文件[ source ]中。
从 NumPy 1.16 版开始,有关更多详细信息,请参阅GitHub 票证 12251。
from sys import maxsize
from numpy import set_printoptions
set_printoptions(threshold=maxsize)
Run Code Online (Sandbox Code Playgroud)
稍微修改一下:(因为你要打印一个巨大的列表)
import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)
x = np.arange(1000)
print(x)
Run Code Online (Sandbox Code Playgroud)
这将增加每行的字符数(默认线宽为 75)。为适合您的编码环境的线宽使用您喜欢的任何值。这将使您不必通过每行添加更多字符来处理大量输出行。
with np.printoptions(edgeitems=50):
print(x)
Run Code Online (Sandbox Code Playgroud)
将 50 更改为您想查看的行数
来源:这里
| 归档时间: |
|
| 查看次数: |
477231 次 |
| 最近记录: |