Chris Rigano
Chris Rigano

Reputation: 701

Making scientific notation readable from a numpy array

How do I convert an array like:

array([ -9.8737e+13, -9.8737e+13, -1.1265e+14, 1.5743e-01, 1.1265e+14, 9.8737e+13, 9.8737e+13])

into a readable form in numpy or python?

Thanks!

Chris

Upvotes: 2

Views: 3908

Answers (1)

unutbu
unutbu

Reputation: 879481

Your array contains both large and small values. It's hard to present both in a readable way. If you use scientific notation the numbers can be shown in a compact form, but it's hard to tell at a glance which numbers are large and which are small.

Alternatively, you could display the floats without scientific notation, for example, like this:

In [132]: np.set_printoptions(formatter={'float_kind':'{:25f}'.format})

In [133]: x
Out[133]: 
array([   -98737000000000.000000,    -98737000000000.000000,
         -112650000000000.000000,                  0.157430,
          112650000000000.000000,     98737000000000.000000,
           98737000000000.000000])

which makes it easy to distinguish the large from the small, but now the eyes boggle looking at too many zeros.

After a while, you may want to go back to NumPy's default format, which you can do by calling np.set_printoptions() without arguments.

In [134]: np.set_printoptions()

In [135]: x
Out[135]: 
array([ -9.8737e+13,  -9.8737e+13,  -1.1265e+14,   1.5743e-01,
         1.1265e+14,   9.8737e+13,   9.8737e+13])

Whatever the case, the above shows you how you can configure NumPy to display floats (or other types) any way you wish. See the docs for more on all the available options.

Upvotes: 4

Related Questions