Numpy Display Options: Examples and Reference

Numpy Display Options: Examples and Reference

Last updated:
Table of Contents

Version used: Numpy 1.21.2. Full code on this jupyter notebook

Show full arrays

Arrays having more than threshold will get truncated.

import numpy as np

# default value is 1000
np.set_printoptions(threshold=2000)

np.array(range(5000))

before-set-printoptions By default, arrays with more
than 1000 elements get truncated
  
after-set-option After setting threshold to 2000,
all elements get printed

Show more edge items

edgeitems controls the number of elements shown when truncation is triggered

import numpy as np

# show 5 elements on either side
np.set_printoptions(edgeitems=5)

before-set-printoptions By default, truncated arrays show
3 elements at either side
  
after-set-printoptions After setting edgeitems to 5,
5 elements on either side
get printed

Set options temporarily

Use a with clause:

import numpy as np

with np.printoptions(edgeitems=5):
    # numpy code here uses the given
    # options

# other code is under default options

Reset options

There is no way to reset the options.

You need to set them to the default values explicitly:

import numpy as np

# setting options to default values explicitly
np.set_printoptions(edgeitems=3, infstr='inf',
linewidth=75, nanstr='nan', precision=8,
suppress=False, threshold=1000, formatter=None)

Dialogue & Discussion