2

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

1
  • 3
    Looks readable to me as it is. :^) What format are you looking for? Commented Feb 20, 2014 at 3:43

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to enable scientific notation only for small numbers? e.g. it would look like [9.8e-10, 0.54]
@Leo: np.set_printoptions(formatter={'float':'{:g}'.format}) would work in that case. See the precision type table in the string formatting docs for info on what the g "general format" does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.