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

其他回答

使用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会自动跳过数组的中心部分,只打印角:要禁用此行为并强制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文档中的“或部分”以获得更多帮助。

临时设置

您可以使用printoptions上下文管理器:

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

(当然,如果导入numpy的方式是用np替换numpy)

使用上下文管理器(with块)可以确保上下文管理器完成后,打印选项将恢复到块开始之前的状态。它确保设置是临时的,并且仅应用于块内的代码。

有关上下文管理器及其支持的其他参数的详细信息,请参阅numpy.printoptions文档。它在NumPy 1.15(发布于2018-07-23)中推出。

作为对最大列数的补充(使用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)

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

对于那些使用崇高文本并希望在输出窗口中看到结果的用户,应该将构建选项“word_wrap”:false添加到崇高构建文件[source]中。

这是最黑客的解决方案,它甚至可以像numpy一样打印得很好:

import numpy as np

a = np.arange(10000).reshape(250,40)

b = [str(row) for row in a.tolist()]

print('\n'.join(b))

Out: