首页 文章

如何打印完整的NumPy阵列?

提问于
浏览
380

当我打印一个numpy数组时,我得到一个截断的表示,但我想要完整的数组 .

有没有办法做到这一点?

Examples:

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

13 回答

  • 29

    假设你有一个numpy数组

    arr = numpy.arange(10000).reshape(250,40)
    

    如果你想以一次性的方式打印完整的数组(没有切换np.set_printoptions),但想要比上下文管理器更简单(更少的代码),只需要做

    for row in arr:
         print row
    
  • 36

    澄清里德的回复

    import numpy
    numpy.set_printoptions(threshold=numpy.nan)
    

    请注意,上面给出的回复适用于初始 from numpy import * ,这是不可取的 .

    有关完整文档,请参阅http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html .

  • 25
    import numpy as np
    np.set_printoptions(threshold=np.inf)
    

    我建议使用 np.inf 而不是其他人建议的 np.nan . 它们都可以用于您的目的,但是通过将阈值设置为"infinity",每个人都可以阅读您的代码,这是显而易见的 . 有一个"not a number"的门槛对我来说似乎有点模糊 .

  • 406

    您可以使用 array2string 函数 - docs .

    a = numpy.arange(10000).reshape(250,40)
    print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))
    # [Big output]
    
  • 50

    如果数组太大而无法打印,NumPy会自动跳过数组的中心部分并仅打印角:要禁用此行为并强制NumPy打印整个数组,可以使用 set_printoptions 更改打印选项 .

    >>> np.set_printoptions(threshold='nan')
    

    or

    >>> np.set_printoptions(edgeitems=3,infstr='inf',
    ... linewidth=75, nanstr='nan', precision=8,
    ... suppress=False, threshold=1000, formatter=None)
    

    您也可以参考numpy documentation numpy documentation for "or part"获取更多帮助 .

  • 2

    NumPy 1.15或更新

    如果您使用NumPy 1.15(2018-07-23发布)或更新版本,则可以使用 printoptions 上下文管理器:

    with numpy.printoptions(threshold=numpy.inf):
        print(arr)
    

    (当然,如果您导入 numpy 的话,请将 numpy 替换为 np

    使用上下文管理器( with -block)可确保在上下文管理器完成后,打印选项将恢复为块启动之前的任何内容 . 它确保设置是临时的,并且仅应用于块内的代码 .

    有关上下文管理器的详细信息以及它支持的其他参数,请参阅numpy.printoptions documentation .

  • 162

    从最大列数(用 numpy.set_printoptions(threshold=numpy.nan) 固定)补充此answer,还有一个字符限制要显示 . 在某些环境中,比如从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)
    

    在这种情况下,您的窗口应限制换行的字符数 .

    对于那些使用sublime文本并希望在输出窗口中查看结果的人,您应该将构建选项 "word_wrap": false 添加到sublime-build文件[source] .

  • 1

    使用上下文管理器作为Paul Price sugggested

    import numpy as np
    
    
    class fullprint:
        'context manager for printing full numpy arrays'
    
        def __init__(self, **kwargs):
            if 'threshold' not in kwargs:
                kwargs['threshold'] = np.nan
            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)
    
  • 8

    这是一个小小的修改(删除了将附加参数传递给neokneok答案的选项 .

    它显示了如何使用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)
    

    在您的代码中,它可以像这样使用:

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

    这听起来像你正在使用numpy .

    如果是这种情况,您可以添加:

    import numpy as np
    np.set_printoptions(threshold=np.nan)
    

    这将禁用角落打印 . 有关更多信息,请参阅NumPy Tutorial .

  • 3

    之前的答案是正确的,但作为一个较弱的替代方案,您可以转换为列表:

    >>> 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]]
    
  • 9

    numpy.savetxt

    numpy.savetxt(sys.stdout, numpy.arange(10000))
    

    或者如果你需要一个字符串:

    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 .

  • 2

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

    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)
    

相关问题