当我打印一个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,请尝试使用变量检查器扩展。您可以单击每个变量以查看整个数组。

其他回答

关闭并返回正常模式

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.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一样打印得很好:

import numpy as np

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

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

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

Out:

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

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

来源:此处