当我打印一个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]])

当前回答

如果您正在使用Jupyter,请尝试使用变量检查器扩展。您可以单击每个变量以查看整个数组。

其他回答

如果你使用的是jupyter笔记本,我发现这是一次性使用的最简单的解决方案。基本上将numpy数组转换为列表,然后转换为字符串,然后打印。这样做的好处是在数组中保留逗号分隔符,而使用numpyp.printoptions(threshold=np.inf)则不会:

import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))

听起来你在用numpy。

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

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

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

with np.printoptions(edgeitems=50):
    print(x)

将50更改为您想看到的行数

来源:此处

关闭并返回正常模式

np.set_printoptions(threshold=False)

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