标签云

微信群

扫码加入我们

WeChat QR Code

When I print a numpy array, I get a truncated representation, but I want the full array. Is there any way to do this?Examples:>>> 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]])


Is there a way to do it on a "one off" basis?That is, to print out the full output once, but not at other times in the script?

2019年05月24日59分02秒

Matt O'Brien see ZSG's answer below

2019年05月24日59分02秒

Could you change the accepted answer to the one recommending np.inf? np.nan and 'nan' only work by total fluke, and 'nan' doesn't even work in Python 3 because they changed the mixed-type comparison implementation that threshold='nan' depended on.

2019年05月23日59分02秒

(threshold=np.nan rather than 'nan' depends on a different fluke, which is that the array printing logic compares the array size to the threshold with a.size > _summaryThreshold. This always returns False for _summaryThreshold=np.nan. If the comparison had been a.size <= _summaryThreshold, testing whether the array should be fully printed instead of testing whether it should be summarized, this threshold would trigger summarization for all arrays.)

2019年05月24日59分02秒

A "one-off" way of doing it:If you have a numpy.array tmp just list(tmp).Other options with different formatting are tmp.tolist() or for more control print("\n".join(str(x) for x in tmp)).

2019年05月24日59分02秒

What's the inverse operation of this? How to go back to the previous setting (with the dots)?

2019年05月24日59分02秒

Karlo The default number is 1000, so np.set_printoptions(threshold=1000) will revert it to default behaviour. But you can set this threshold as low or high as you like. np.set_printoptions(threshold=np.inf) simply changes the maximum size a printed array can be before it is truncated to infinite, so that it is never truncated no matter how big. If you set the threshold to any real number then that will be the maximum size.

2019年05月23日59分02秒

Not only is this clearer, it's much less fragile. There is no special handling for np.inf, np.nan, or 'nan'. Whatever you put there, NumPy will still use a plain > to compare the size of the array to your threshold. np.nan only happens to work because it's a.size > _summaryThreshold instead of a.size <= _summaryThreshold, and np.nan returns False for all >/</>=/<= comparisons. 'nan' only happens to work due to fragile implementation details of Python 2's mixed-type comparison logic; it breaks completely on Python 3.

2019年05月23日59分02秒

Use sys.maxsize since the value is documented to be an int

2019年05月23日59分02秒

This seems to be the best one-off way to see your full array in a print statement.

2019年05月24日59分02秒

ValueError: threshold must be numeric and non-NAN, try sys.maxsize for untruncated representation

2019年05月24日59分02秒

Looks like this would be a good place to use a context manager, so you can say "with fullprint".

2019年05月23日59分02秒

Do not use 'nan', np.nan, or any of the above. It's unsupported, and this bad advice is causing pain for people transitioning to python 3

2019年05月24日59分02秒

ZSG Replace line 5 with numpy.set_printoptions(threshold=numpy.inf)

2019年05月23日59分02秒

That's a smart use of a context manager.

2019年05月24日59分02秒

This context manager is built into numpy 1.15, thanks to github.com/numpy/numpy/pull/10406, under the name np.printoptions

2019年05月24日59分02秒

This is currently the cleanest answer, the others are sort of outdated.

2019年05月24日59分02秒

This is fantastic, nice and short and works really well.

2019年05月24日59分02秒

You should always put a try / finally around the yield in a context manager, so that the cleanup happens no matter what.

2019年05月23日59分02秒

Eric indeed. Thank you for your helpful comment and I have updated the answer.

2019年05月24日59分02秒

In 1.15, this can be spelt with np.printoptions(threshold=np.inf):

1970年01月01日00分01秒

Do not use 'nan', np.nan, or any of the above. It's unsupported, and this bad advice is causing pain for people transitioning to python 3

2019年05月24日59分02秒