Python 如何在不截断的情况下打印完整的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数组时,我会得到一个截断的表示,但我想要完整的数组

有没有办法做到这一点

示例:

>>> 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)

这将禁用角打印。有关更多信息,请参见此。

这听起来像是在使用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)

以下是一种一次性方法,如果您不想更改默认设置,此方法非常有用:

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
。它们都是为您的目的而工作的,但是通过将阈值设置为“无穷大”,每个阅读您的代码的人都会明白您的意思。“不是一个数字”的门槛对我来说似乎有点模糊


我建议使用
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]]

前面的答案是正确的,但作为一个较弱的选择,您可以转换为一个列表:

>>> 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)

使用上下文管理器作为建议

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.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')


您还可以参考以获取更多帮助。

如果阵列太大而无法打印,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)

这是对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 

假设您有一个numpy数组

 arr = numpy.arange(10000).reshape(250,40)
如果您想一次性打印完整数组(无需切换np.set\u printpoptions),但需要比上下文管理器更简单(代码更少),只需执行以下操作即可

for row in arr:
     print row 

您可以使用
array2string
函数-


您可以使用
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.set\u printpoptions(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

使用上下文管理器(带-块的
)可确保上下文管理器完成后,打印选项将恢复到块启动前的状态。它确保设置是临时的,并且仅应用于