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

当前回答

关闭并返回正常模式

np.set_printoptions(threshold=False)

其他回答

前面的答案是正确的,但作为一个较弱的选择,您可以转换为列表:

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

假设您有一个numpy数组

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

如果您想一次性打印整个数组(不切换np.set_printoptions),但想要比上下文管理器更简单(更少的代码),只需执行以下操作

for row in arr:
     print row 

关闭并返回正常模式

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

我建议使用np.inf而不是其他人建议的np.nan。它们都符合您的目的,但通过将阈值设置为“无限”,每个阅读代码的人都会明白您的意思。对我来说,“不是数字”的门槛似乎有点模糊。