LoginSignup
48
26

More than 5 years have passed since last update.

numpy.arrayをいい感じにprintするノート

Posted at

親切なnumpy array

リストの中身によって表示を変えてくれる神仕様。

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]

ちょっと困ります。。。

np.set_printoptions()で表示のフォーマットを指定できる。
上から順に強い制約を加えていく。

test.py
np.set_printoptions(precision=3) #有効桁3桁で丸める
[ 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) #指数表示の禁止
[ 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}) #桁を揃える
[ 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]

参考URL

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

48
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
48
26