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

其他回答

从NumPy 1.16版本开始,有关更多详细信息,请参阅GitHub票证12251。

from sys import maxsize
from numpy import set_printoptions

set_printoptions(threshold=maxsize)

numpy.savetxt

numpy.savetxt(sys.stdout, numpy.arange(10000))

或者如果需要字符串:

import StringIO
sio = StringIO.StringIO()
numpy.savetxt(sio, numpy.arange(10000))
s = sio.getvalue()
print s

默认输出格式为:

0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
...

并且可以用其他参数来配置它。

请特别注意,这也不会显示方括号,并允许进行大量自定义,如:how to print a Numpy array without bracket?

在Python 2.7.12,numpy 1.11.1上测试。

如果你有熊猫,

    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.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]中。

临时设置

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

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

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

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

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