58

How can I print numpy array with 3 decimal places? I tried array.round(3) but it keeps printing like this 6.000e-01. Is there an option to make it print like this: 6.000?

I got one solution as print ("%0.3f" % arr), but I want a global solution i.e. not doing that every time I want to check the array contents.

djvg
  • 11,722
  • 5
  • 72
  • 103
Jack Twain
  • 6,273
  • 15
  • 67
  • 107

3 Answers3

76
 np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})

This will set numpy to use this lambda function for formatting every float it prints out.

other types you can define formatting for (from the docstring of the function)

    - 'bool'
    - 'int'
    - 'timedelta' : a `numpy.timedelta64`
    - 'datetime' : a `numpy.datetime64`
    - 'float'
    - 'longfloat' : 128-bit floats
    - 'complexfloat'
    - 'longcomplexfloat' : composed of two 128-bit floats
    - 'numpy_str' : types `numpy.string_` and `numpy.unicode_`
    - 'str' : all other strings

Other keys that can be used to set a group of types at once are::

    - 'all' : sets all types
    - 'int_kind' : sets 'int'
    - 'float_kind' : sets 'float' and 'longfloat'
    - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat'
    - 'str_kind' : sets 'str' and 'numpystr'
djvg
  • 11,722
  • 5
  • 72
  • 103
M4rtini
  • 13,186
  • 4
  • 35
  • 42
  • 2
    sorry I removed the acceptance. np.set_printoptions() doesn't accept a formatter! – Jack Twain Mar 07 '14 at 10:46
  • 1
    If it won't accept a formatter, you might be running a old version of numpy. I'm not sure when this feature was added. But it is documented, so it is indeed supported: http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html – M4rtini Mar 07 '14 at 11:24
  • 6
    Worth noting you don't need the lambda: `np.set_printoptions(formatter={'float': "{0:0.3f}".format})` will work, since format is already a function. – Salvatore May 12 '20 at 21:28
  • Still works in 1.23.1. Note there's also a context-manager for this: [numpy.printoptions](https://numpy.org/doc/stable/reference/generated/numpy.printoptions.html#numpy.printoptions). – djvg Sep 01 '22 at 08:26
  • 1
    The context manager can be used as: with np.printoptions(precision=2): print(an_array) – Heberto Mayorquin Sep 03 '22 at 21:35
48

Actually what you need is np.set_printoptions(precision=3). There are a lot of helpful other parameters there.

For example:

np.random.seed(seed=0)
a = np.random.rand(3, 2)
print a
np.set_printoptions(precision=3)
print a

will show you the following:

[[ 0.5488135   0.71518937]
 [ 0.60276338  0.54488318]
 [ 0.4236548   0.64589411]]
[[ 0.549  0.715]
 [ 0.603  0.545]
 [ 0.424  0.646]]
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 6
    Your example works. But I don't know why in my jupyter notebook, it still shows something like ```[ 5.000e-02 2.139e-01 1.028e+00 ..., 1.804e+03 1.805e+03 1.806e+03]``` – nngeek Nov 30 '17 at 03:26
  • 10
    In the end, this works: ```np.set_printoptions(precision=3, suppress=True)```, since small value could be suppressed. Refer to the doc [here](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.set_printoptions.html). – nngeek Nov 30 '17 at 03:34
34

An easier solution is to use numpy around.

>>> randomArray = np.random.rand(2,2)
>>> print(randomArray)
array([[ 0.07562557,  0.01266064],
   [ 0.02759759,  0.05495717]])
>>> print(np.around(randomArray,3))
[[ 0.076  0.013]
 [ 0.028  0.055]]
Thomas
  • 603
  • 7
  • 12