925

When I print a numpy array, I get a truncated representation, but I want the full array.

>>> 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]])
Mateen Ulhaq
27.8k21 gold badges121 silver badges155 bronze badges
asked Jan 1, 2010 at 1:51
8
  • 45
    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? Commented May 18, 2014 at 3:07
  • 4
    @Matt O'Brien see ZSG's answer below Commented Aug 8, 2014 at 21:04
  • 7
    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. Commented Jun 1, 2017 at 20:03
  • 1
    (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.) Commented Jun 1, 2017 at 20:14
  • 9
    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)). Commented Dec 30, 2017 at 2:17

23 Answers 23

1006

Use numpy.set_printoptions:

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
Sign up to request clarification or add additional context in comments.

8 Comments

if you only want to print a numpy array only once, unfortunately this solution has the downside of requiring you to reset this configuration change after doing the print.
@TrevorBoydSmith, Do you know how to reset this parameter after the print?
@ColinMac see stackoverflow.com/a/24542498/52074 where he saves the settings. does an operation. then restores the settings.
And how to reset it back to normal?
@Gulzar use: numpy.set_printoptions(threshold = False)
|
324
import numpy as np
np.set_printoptions(threshold=np.inf)

I suggest using np.inf instead of np.nan which is suggested by others. They both work for your purpose, but by setting the threshold to "infinity" it is obvious to everybody reading your code what you mean. Having a threshold of "not a number" seems a little vague to me.

answered Sep 24, 2014 at 14:01

5 Comments

What's the inverse operation of this? How to go back to the previous setting (with the dots)?
@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.
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.
Use sys.maxsize since the value is documented to be an int
To properly answer @Karlo's question, note that the initial value for the print options threshold is found in np.get_printoptions()['threshold']. You can store this value before setting the threshold and then restore it afterwards (or use a with block as suggested in other answers).
274

Temporary setting

You can use the printoptions context manager:

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

(of course, replace numpy by np if that's how you imported numpy)

The use of a context manager (the with-block) ensures that after the context manager is finished, the print options will revert to whatever they were before the block started. It ensures the setting is temporary, and only applied to code within the block.

See numpy.printoptions documentation for details on the context manager and what other arguments it supports. It was introduced in NumPy 1.15 (released 2018年07月23日).

answered Nov 23, 2018 at 18:30

1 Comment

Best one-off solution for preserving the row/column orientation that converting to list destroys.
181

The previous answers are the correct ones, but as a weaker alternative you can transform into a list:

>>> 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]]
Eric
98.1k54 gold badges257 silver badges389 bronze badges
answered Apr 23, 2015 at 13:40

4 Comments

This seems to be the best one-off way to see your full array in a print statement.
@AaronBramson i agree... this is less-error prone when you need just one print statement (one line of code as opposed to 3 lines for: change config, print, reset config).
I like that this prints the comma separators
This solution is great for integers but less great for doubles
54

Here is a one-off way to do this, which is useful if you don't want to change your default settings:

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)
Paul Rougieux
11.5k5 gold badges86 silver badges132 bronze badges
answered Jul 2, 2014 at 23:08

1 Comment

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

This sounds like you're using numpy.

If that's the case, you can add:

import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)

That will disable the corner printing. For more information, see this NumPy Tutorial.

Jason R
11.9k10 gold badges58 silver badges90 bronze badges
answered Jan 1, 2010 at 2:02

2 Comments

ValueError: threshold must be numeric and non-NAN, try sys.maxsize for untruncated representation
Yes, That part of the official Numpy tutorial is wrong
38

Using a context manager as Paul Price sugggested

import numpy as np
class fullprint:
 'context manager for printing full numpy arrays'
 def __init__(self, **kwargs):
 kwargs.setdefault('threshold', np.inf)
 self.opt = kwargs
 def __enter__(self):
 self._opt = np.get_printoptions()
 np.set_printoptions(**self.opt)
 def __exit__(self, type, value, traceback):
 np.set_printoptions(**self._opt)
if __name__ == '__main__': 
 a = np.arange(1001)
 with fullprint():
 print(a)
 print(a)
 with fullprint(threshold=None, edgeitems=10):
 print(a)
answered Sep 9, 2016 at 6:33

1 Comment

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

numpy.savetxt

numpy.savetxt(sys.stdout, myarray)

or if you need a string:

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

For example testing with:

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

the default output format is:

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

and it can be configured with further arguments.

Note in particular how this also not shows the square brackets, and allows for a lot of customization, as mentioned at: How to print a Numpy array without brackets?

A downside of this method is that it only works for 1D or 2D arrays, 3D or greater blows up with:

ValueError: Expected 1D or 2D array, got 3D array instead

Tested on Python 2.7.12, numpy 1.11.1.

answered Feb 4, 2017 at 23:22

2 Comments

minor drawback to this method is that in only works with 1d and 2d arrays
@Fnord thanks for this info, let me know if you find a workaround!
16

A slight modification: (since you are going to print a huge list)

import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)
x = np.arange(1000)
print(x)

This will increase the number of characters per line (default linewidth of 75). Use any value you like for the linewidth which suits your coding environment. This will save you from having to go through huge number of output lines by adding more characters per line.

answered Apr 23, 2020 at 8:13

1 Comment

Thank you for a great tip. linewidth is extremely useful when you want to print a large matrix and avoid line breaks.
13

This is a slight modification (removed the option to pass additional arguments to set_printoptions)of neoks answer.

It shows how you can use contextlib.contextmanager to easily create such a contextmanager with fewer lines of code:

import numpy as np
from contextlib import contextmanager
@contextmanager
def show_complete_array():
 oldoptions = np.get_printoptions()
 np.set_printoptions(threshold=np.inf)
 try:
 yield
 finally:
 np.set_printoptions(**oldoptions)

In your code it can be used like this:

a = np.arange(1001)
print(a) # shows the truncated array
with show_complete_array():
 print(a) # shows the complete array
print(a) # shows the truncated array (again)
answered Aug 23, 2017 at 5:43

3 Comments

You should always put a try / finally around the yield in a context manager, so that the cleanup happens no matter what.
@Eric indeed. Thank you for your helpful comment and I have updated the answer.
In 1.15, this can be spelt with np.printoptions(threshold=np.inf):
13
with np.printoptions(edgeitems=50):
 print(x)

Change 50 to how many lines you wanna see

Source: here

answered Jan 20, 2021 at 17:36

Comments

7

Complementary to this answer from the maximum number of columns (fixed with numpy.set_printoptions(threshold=numpy.nan)), there is also a limit of characters to be displayed. In some environments like when calling python from bash (rather than the interactive session), this can be fixed by setting the parameter linewidth as following.

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)

In this case, your window should limit the number of characters to wrap the line.

For those out there using sublime text and wanting to see results within the output window, you should add the build option "word_wrap": false to the sublime-build file [source] .

answered Sep 28, 2018 at 6:34

Comments

7

To turn it off and return to the normal mode

np.set_printoptions(threshold=False)
answered May 24, 2019 at 6:12

3 Comments

It works for me (Jupyter python version 3). You may try the code below.As per the official documnetation the code below should put back to the default options. Which it did for me too. >np.set_printoptions(edgeitems=3,infstr='inf', linewidth=75, nanstr='nan', precision=8, suppress=False, threshold=1000, formatter=None)
Okay, it must be because I'm not using Jupyter. The accepted answer does work for me in a pure python environment though.
This means threshold=0, which means "truncate as soon as possible" - not what you want at all.
6

Since NumPy version 1.16, for more details see GitHub ticket 12251.

from sys import maxsize
from numpy import set_printoptions
set_printoptions(threshold=maxsize)
Georgy
13.9k7 gold badges69 silver badges79 bronze badges
answered Jan 21, 2019 at 20:12

Comments

4

matrepr will print the entire array with disabled max_rows and max_cols limits:

from matrepr import mprint
a = numpy.arange(10000).reshape(250,40)
mprint(a, max_rows=None, max_cols=None)

First few lines of the result:

&l×ばつ40, 10000 'int64' elements, array>
 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
 ┌ ┐
 0 │ 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 │
 1 │ 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 │
 2 │ 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 │
 3 │ 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 │
 4 │ 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 │
 5 │ 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 │
 6 │ 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 │
 7 │ 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 │
 8 │ 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 │
 9 │ 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 │
 10 │ 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 │
 11 │ 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 │
answered Sep 13, 2023 at 3:23

2 Comments

Very cool did you write it?
@SashSinha yes.
3

Suppose you have a numpy array

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

If you want to print the full array in a one-off way (without toggling np.set_printoptions), but want something simpler (less code) than the context manager, just do

for row in arr:
 print row 
answered May 2, 2018 at 18:33

Comments

3

If you're using a jupyter notebook, I found this to be the simplest solution for one off cases. Basically convert the numpy array to a list and then to a string and then print. This has the benefit of keeping the comma separators in the array, whereas using numpyp.printoptions(threshold=np.inf) does not:

import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))
answered Jan 12, 2021 at 22:47

Comments

3

This is the hackiest solution it even prints it nicely as numpy does:

import numpy as np
a = np.arange(10000).reshape(250,40)
b = [str(row) for row in a.tolist()]
print('\n'.join(b))

Out:

output in terminal

answered Nov 9, 2022 at 5:11

Comments

2

You can use the array2string function - docs.

a = numpy.arange(10000).reshape(250,40)
print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))
# [Big output]
answered Aug 3, 2018 at 13:01

1 Comment

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

You won't always want all items printed, especially for large arrays.

A simple way to show more items:

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])

It works fine when sliced array < 1000 by default.

answered Mar 6, 2019 at 5:35

Comments

2

If you are using Jupyter, try the variable inspector extension. You can click each variable to see the entire array.

answered Dec 23, 2020 at 22:39

Comments

1

If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners: 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')

or

>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)

You can also refer to the numpy documentation numpy documentation for "or part" for more help.

Glorfindel
22.8k13 gold badges97 silver badges124 bronze badges
answered Jul 1, 2017 at 13:27

2 Comments

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
ValueError: threshold must be numeric and non-NAN, try sys.maxsize for untruncated representation
0

If you have pandas available,

 numpy.arange(10000).reshape(250,40)
 print(pandas.DataFrame(a).to_string(header=False, index=False))

avoids the side effect of requiring a reset of numpy.set_printoptions(threshold=sys.maxsize) and you don't get the numpy.array and brackets. I find this convenient for dumping a wide array into a log file

answered May 19, 2020 at 3:45

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.