43

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.

Salvador Dali
  • 199,541
  • 138
  • 677
  • 738
Jack Twain
  • 5,933
  • 11
  • 59
  • 105

3 Answers3

63

Warning: this answer no longer works as of 2022. See @Salvador Dali's answer for the correct way to do this in 2022.

----------------

 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'
Michael Currie
  • 12,486
  • 8
  • 41
  • 56
M4rtini
  • 12,568
  • 3
  • 32
  • 41
  • 1
    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
  • 2
    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. – Viglione May 12 '20 at 21:28
45

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
  • 199,541
  • 138
  • 677
  • 738
  • 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
  • 8
    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
27

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
  • 463
  • 6
  • 12