Long NumPy array can't be printed completely?

↘锁芯ラ 提交于 2019-12-01 06:52:10

问题


I'm trying to print the complete contents of two 1001x1 arrays, but Python only gives me truncated output something like this:

array([[5,45],
       [1,23],
       ......,
       [1,24],
       [2,31]])  

instead of the complete array.

Can anyone give me solution of how to get the complete 1001x1 array?


回答1:


I'm going to guess that you tried a simple statement like:

print myarray

... rather than something more explicit like:

for each_item in myarray:
    print each_item

... or even:

print ', '.join([str(x) for x in myarray])

The reason you're seeing elided output is, presumably, because numpy implements a _str_ method in its array class which tries to give a "reaasonable" default string representation of the array. They are, presumably, assuming that simple print statements will be used primarily for debugging, logging, or similar purposes and that reporting of results, or marshaling of results to other processes or storage, is going to be done using more explicit iterations over the data (as I've shown here).




回答2:


See the section Printing Arrays in the NumPy tutorial:

If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners:

>>> print(np.arange(10000))
[   0    1    2 ..., 9997 9998 9999]

...

To disable this behaviour and force NumPy to print the entire array, you can change the printing options using set_printoptions.

>>> np.set_printoptions(threshold=nan)

The np.set_printoptions function is part of the NumPy library.



来源:https://stackoverflow.com/questions/4528612/long-numpy-array-cant-be-printed-completely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!