Python 如何在不截断的情况下打印完整的NumPy数组?
当我打印一个numpy数组时,我会得到一个截断的表示,但我想要完整的数组 有没有办法做到这一点 示例:Python 如何在不截断的情况下打印完整的NumPy数组?,python,arrays,numpy,options,output-formatting,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,
>>> 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]])
这听起来像是你在用numpy 如果是这种情况,您可以添加:
import numpy as np
np.set_printoptions(threshold=np.nan)
这将禁用角打印。有关更多信息,请参见此。使用:
以下是一种一次性方法,如果您不想更改默认设置,此方法非常有用:
def fullprint(*args, **kwargs):
from pprint import pprint
import numpy
opt = numpy.get_printoptions()
numpy.set_printoptions(threshold=numpy.inf)
pprint(*args, **kwargs)
numpy.set_printoptions(**opt)
我建议使用
np.inf
而不是别人建议的np.nan
。它们都是为您的目的而工作的,但是通过将阈值设置为“无穷大”,每个阅读您的代码的人都会明白您的意思。“不是一个数字”的阈值对我来说似乎有点模糊。前面的答案是正确的,但作为一个较弱的选择,您可以转换为一个列表:
>>> 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]]
使用上下文管理器作为建议
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)
if __name__ == '__main__':
a = np.arange(1001)
with fullprint():
print(a)
print(a)
with fullprint(threshold=None, edgeitems=10):
print(a)
numpy.savetxt
numpy.savetxt(sys.stdout, numpy.arange(10000))
>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)
或者,如果需要字符串:
import StringIO
sio = StringIO.StringIO()
numpy.savetxt(sio, numpy.arange(10000))
s = sio.getvalue()
print s
默认输出格式为:
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
...
它还可以配置其他参数
请特别注意,这也没有显示方括号,并允许进行大量定制,如所述:
在Python 2.7.12和numpy 1.11.1上测试。如果数组太大而无法打印,numpy会自动跳过数组的中心部分,只打印角: 要禁用此行为并强制NumPy打印整个阵列,可以使用
set\u printoptions
更改打印选项
>>> np.set_printoptions(threshold='nan')
或
您还可以参考以获取更多帮助。这是对s答案的一个轻微修改(删除了将附加参数传递给
set\u printoptions)
它显示了如何使用轻松创建这样一个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)
在您的代码中,可以这样使用:
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)
假设您有一个numpy数组
arr = numpy.arange(10000).reshape(250,40)
如果您想一次性打印完整数组(无需切换np.set\u printpoptions),但需要比上下文管理器更简单(代码更少),只需执行以下操作即可
for row in arr:
print row
您可以使用
array2string
函数-
除了最大列数(固定为numpy.set\u 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)
在这种情况下,窗口应该限制换行字符的数量
对于那些使用升华文本并希望在输出窗口中看到结果的用户,您应该将生成选项“word\u wrap”:false
添加到升华生成文件[]中。临时设置
如果您使用NumPy 1.15(2018-07-23发布)或更新版本,您可以使用printoptions
上下文管理器:
with numpy.printoptions(threshold=numpy.inf):
print(arr)
(当然,如果导入的是numpy
,请将numpy
替换为np
)
使用上下文管理器(带-块的)可确保上下文管理器完成后,打印选项将恢复到块启动前的状态。它确保设置是临时的,并且仅应用于块内的代码
有关上下文管理器及其支持的其他参数的详细信息,请参阅。自NumPy版本1.16以来,有关更多详细信息,请参阅
您并不总是希望打印所有项目,尤其是对于大型阵列
显示更多项目的简单方法:
In [349]: ar
Out[349]: array([1, 1, 1, ..., 0, 0, 0])
In [350]: ar[:100]
Out[350]:
array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
当切片阵列默认值<1000时,它工作正常。将其关闭并返回正常模式
np.set_printoptions(threshold=False)
稍作修改:(因为您将要打印一个巨大的列表)
这将增加每行的字符数(默认线宽为75)。为适合您的编码环境的线宽使用您喜欢的任何值。通过在每行中添加更多字符,这将使您不必经历大量的输出行。如果您有熊猫
numpy.arange(10000).reshape(250,40)
print(pandas.DataFrame(a).to_string(header=False, index=False))
避免了需要重置numpy.set\u printoptions(threshold=sys.maxsize)
的副作用,并且您不会得到numpy.array和方括号。我发现将宽数组转储到日志文件中很方便如果您使用的是Jupyter,请尝试扩展名。您可以单击每个变量查看整个数组。如果您使用的是jupyter笔记本,我发现这是一次性情况下最简单的解决方案。基本上将numpy数组转换为列表,然后转换为字符串,然后打印。这样做的好处是将逗号分隔符保留在数组中,而使用numpyp.printpoptions(threshold=np.inf)
则不会:
import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))
将50更改为您想看到的行数
资料来源:是否有一种“一次性”的方法?也就是说,要打印一次完整的输出,但不能在脚本中的其他时间打印?@Matt O'Brien请参见下面ZSG的答案您是否可以将接受的答案更改为推荐的np.inf
np.nan
和'nan'
只能完全靠运气工作,因为它们改变了threshold='nan'
所依赖的混合类型比较实现。(threshold=np.nan
而不是'nan'
取决于另一个侥幸,即数组打印逻辑将数组大小与阈值进行比较。对于\u summaryThreshold=np.nan
,这始终返回False
。如果比较是a.size a“一次性的”方法:如果你有一个numpy.arraytmp
justlist(tmp)
。其他不同格式的选项有tmp.tolist()
或更多控件print(“\n”。join(str(x)代表tmp中的x))
。看起来这是一个使用上下文管理器的好地方,所以你可以说“with fullprint”.这是什么逆运算?如何返回到上一组
In [349]: ar
Out[349]: array([1, 1, 1, ..., 0, 0, 0])
In [350]: ar[:100]
Out[350]:
array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
np.set_printoptions(threshold=False)
import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)
x = np.arange(1000)
print(x)
numpy.arange(10000).reshape(250,40)
print(pandas.DataFrame(a).to_string(header=False, index=False))
import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))
with np.printoptions(edgeitems=50):
print(x)