\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)

Making floating points numbers print nicelyΒΆ

By default, when numpy prints an array, it looks for very small or very large numbers. If it finds either, it uses exponents to show the numbers. This can be annoying:

>>> import numpy as np
>>> np.pi
3.141592653589793
>>> np.array([np.pi, 0.000001])
array([  3.14159265e+00,   1.00000000e-06])

In order to avoid this, you can tell numpy not to use exponential notation for small numbers:

>>> np.set_printoptions(suppress=True)
>>> np.array([np.pi, 0.000001])
array([ 3.14159265,  0.000001  ])

This setting stays in place until you change it:

>>> np.array([np.pi, 0.000001])
array([ 3.14159265,  0.000001  ])

It can also be annoying to see many digits after the decimal point, if you know that these are not important. You can set the number of digits after the decimal point for numpy printing like this:

>>> np.set_printoptions(precision=4)
>>> a = np.array([np.pi, 0.000001])
>>> a
array([ 3.1416,  0.    ])

This only affects printing, not calculations:

>>> b = a * 2
>>> b
array([ 6.2832,  0.    ])
>>> # change the printoptions again, we see more decimal places
>>> np.set_printoptions(precision=8)
>>> b
array([ 6.28318531,  0.000002  ])