A note that prints numpy.array nicely

Kind numpy array

God specification that changes the display depending on the contents of the list.

test.py


import numpy as np
x = np.random.random(10)

[ 0.27995734  0.05977967  0.59401233  0.45374778  0.69411132  0.3680196
  0.57969357  0.52534503  0.9995786   0.33110147]
[  2.41417094e-06   4.15437686e-06   8.80283329e-07   9.19866647e-06
   2.07120169e-07   9.81829578e-06   9.98575051e-06   1.63425382e-06
   6.44888900e-07   5.88317420e-06]
[4 8 7 6 2 4 0 4 0 2]

I'm a little troubled. .. ..

You can specify the display format with np.set_printoptions (). Apply strong constraints in order from the top.

test.py


np.set_printoptions(precision=3) #Round with 3 effective digits
[ 0.014  0.68   0.576  0.57   0.576  0.511  0.943  0.51   0.681  0.869]

np.set_printoptions(precision=3, suppress=True) #Prohibition of exponential notation
[ 0.004  0.006  0.006  0.00   0.001  0.005  0.004  0.006  0.005  0.01 ]

np.set_printoptions(formatter={'float': '{: 0.3f}'.format}) #Align digits
[ 0.885  0.194  0.296  0.005  0.379  0.934  0.477  0.385  0.686  0.348]
[ 9.000  4.000  6.000  1.000  6.000  0.000  1.000  7.000  6.000  9.000]

Reference URL

https://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array

Recommended Posts

A note that prints numpy.array nicely
Just a note
A note about __call__
A note about subprocess
A note about mprotect (2)
A note about KornShell (ksh)
A note about TensorFlow Introduction
A small note following printf
Note that it supports Python 3
A note about [python] __debug__
A Python one-liner that pretty prints json. Do not escape Japanese.
[python] A note that started to understand the behavior of matplotlib.pyplot