Making scientific notation readable from a numpy array

£可爱£侵袭症+ 提交于 2019-12-14 02:09:48

问题


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:


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.



来源:https://stackoverflow.com/questions/21897637/making-scientific-notation-readable-from-a-numpy-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!