当我打印 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.set_printoptions

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

其他提示

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

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

如果是这样的话,你可以添加:

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

这将禁止角打印。有关详细信息,请参阅本 NumPy的教程

下面是一个一次性的方式做到这一点,如果你不希望更改默认设置,这是有用的:

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)

使用上下文管理器作为保罗价格 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)

NumPy的1.15或更高版本

如果您使用NumPy的1.15(2018年7月23日公布),或者较新的,则可以使用printoptions上下文管理器:

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

(当然,通过numpy更换np如果这就是你如何进口numpy

这上下文管理器完成后,打印选项将恢复的用途的上下文管理器(在with块)保证的到任何他们之前的块开始。它确保的设定是暂时的,并且仅在块内应用到代码。

请参阅 numpy.printoptions文档上下文管理的细节和其他什么论据支持。

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

并且它可以配置更多参数。

请特别注意,这也不显示方括号,并允许进行大量自定义,如下所述: 如何打印没有括号的 Numpy 数组?

在 Python 2.7.12、numpy 1.11.1 上测试。

这是一个微小的修饰(除去传递额外的参数选项set_printoptions)of neok 的回答。

它显示了如何使用 contextlib.contextmanager 来容易地创建这样的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.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)

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

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

要关闭它,并返回到正常模式

np.set_printoptions(threshold=False)

假设有一个numpy的阵列

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

如果您想打印在全阵列一次性路(不含切换np.set_printoptions),但希望比上下文管理更简单的东西(更少的代码),只是做

for row in arr:
     print row 

您可以使用 array2string 功能 - 文档.

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

由于NumPy的版本1.16的更多细节,请参见 GitHub的票12251

from sys import maxsize
from numpy import set_printoptions

set_printoptions(threshold=maxsize)

您并不想总是印刷,特别是对于大型阵列的所有项目。

一个简单的方式来显示多个项目:

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时

它工作正常。

如果阵列是太大而不能印刷,NumPy的自动跳过所述阵列的中央部分和只打印角: 禁用此行为,并迫使NumPy的打印整个阵列,则可以使用set_printoptions改变打印选项。

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

<强>

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

您还可以参阅 numpy的文档 numpy的用于 “或部分” 的文档以更多的帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top