当我打印一个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.arange(10000).reshape(250,40)
    print(pandas.DataFrame(a).to_string(header=False, index=False))

避免了需要重置numpy.set_printoptions(threshold=sys.maxsize)的副作用,并且不会得到numpy.array和括号。我发现将一个大数组转储到日志文件中很方便

其他回答

如果数组太大而无法打印,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文档numpy文档中的“或部分”以获得更多帮助。

关闭并返回正常模式

np.set_printoptions(threshold=False)

使用numpy.set_printoptions:

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

使用Paul Price建议的上下文管理器

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.arange(10000).reshape(250,40)
    print(pandas.DataFrame(a).to_string(header=False, index=False))

避免了需要重置numpy.set_printoptions(threshold=sys.maxsize)的副作用,并且不会得到numpy.array和括号。我发现将一个大数组转储到日志文件中很方便