Pretty-Print a Numpy Array Without Scientific Notation and With Given Precision

Pretty-print a NumPy array without scientific notation and with given precision

Use numpy.set_printoptions to set the precision of the output:

import numpy as np
x = np.random.random(10)
print(x)
# [ 0.07837821 0.48002108 0.41274116 0.82993414 0.77610352 0.1023732
# 0.51303098 0.4617183 0.33487207 0.71162095]

np.set_printoptions(precision=3)
print(x)
# [ 0.078 0.48 0.413 0.83 0.776 0.102 0.513 0.462 0.335 0.712]

And suppress suppresses the use of scientific notation for small numbers:

y = np.array([1.5e-10, 1.5, 1500])
print(y)
# [ 1.500e-10 1.500e+00 1.500e+03]

np.set_printoptions(suppress=True)
print(y)
# [ 0. 1.5 1500. ]

To apply print options locally, using NumPy 1.15.0 or later, you could use the numpy.printoptions context manager.
For example, inside the with-suite precision=3 and suppress=True are set:

x = np.random.random(10)
with np.printoptions(precision=3, suppress=True):
print(x)
# [ 0.073 0.461 0.689 0.754 0.624 0.901 0.049 0.582 0.557 0.348]

But outside the with-suite the print options are back to default settings:

print(x)    
# [ 0.07334334 0.46132615 0.68935231 0.75379645 0.62424021 0.90115836
# 0.04879837 0.58207504 0.55694118 0.34768638]

If you are using an earlier version of NumPy, you can create the context manager
yourself. For example,

import numpy as np
import contextlib

@contextlib.contextmanager
def printoptions(*args, **kwargs):
original = np.get_printoptions()
np.set_printoptions(*args, **kwargs)
try:
yield
finally:
np.set_printoptions(**original)

x = np.random.random(10)
with printoptions(precision=3, suppress=True):
print(x)
# [ 0.073 0.461 0.689 0.754 0.624 0.901 0.049 0.582 0.557 0.348]

To prevent zeros from being stripped from the end of floats:

np.set_printoptions now has a formatter parameter which allows you to specify a format function for each type.

np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
print(x)

which prints

[ 0.078  0.480  0.413  0.830  0.776  0.102  0.513  0.462  0.335  0.712]

instead of

[ 0.078  0.48   0.413  0.83   0.776  0.102  0.513  0.462  0.335  0.712]

Suppress Scientific Notation in Numpy When Creating Array From Nested List

This is what you need:

np.set_printoptions(suppress=True)

Here is the documentation.

How to printing numpy array with 3 decimal places?

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'

how to force the data type in a numpy array away from scientific notiation

What you are looking for is:

np.set_printoptions(suppress=True)

However, keep that in mind that it is just printing style. Internally, they are all stored in floating point (includes mantissa and exponent) format. Also, note that if the ratio of largest number to smallest number is larger than mantissa size can handle (which I think is around 51 bits), it is going to force scientific notation even with setting suppress=True.

sample code:

a = np.array([1.234,0.0000002, 1000000])

np.set_printoptions(suppress=True)
[ 1.234 0.0000002 1000000. ]

You can add floatmode argument to fill in the blanks with 0s (it sets different styles of printing float numbers):

np.set_printoptions(suppress=True,floatmode='maxprec_equal')
[ 1.2340000 0.0000002 100000.0000000]

or

np.set_printoptions(suppress=True,floatmode='fixed')
[ 1.23400000 0.00000020 100000.00000000]

and if you add precision to it:

np.set_printoptions(suppress=True,floatmode='maxprec',precision=2)
[ 1.23 0. 100000. ]


Related Topics



Leave a reply



Submit