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

当前回答

您不会总是想要打印所有项目,尤其是对于大型阵列。

显示更多项目的简单方法:

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时,它工作正常。

其他回答

临时设置

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

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

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

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

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

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

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

稍微修改一下:(因为你要打印一个巨大的列表)

import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)

x = np.arange(1000)
print(x)

这将增加每行的字符数(默认线宽为75)。使用适合编码环境的任何线宽值。这将通过每行添加更多的字符来避免您必须经过大量的输出行。

这里有一个一次性的方法,如果您不想更改默认设置,这很有用:

def fullprint(*args, **kwargs):
  from pprint import pprint
  import numpy
  opt = numpy.get_printoptions()
  numpy.set_printoptions(threshold=numpy.inf)
  pprint(*args, **kwargs)
  numpy.set_printoptions(**opt)

假设您有一个numpy数组

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

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

for row in arr:
     print row