How to pretty-print a numpy.array without scientific notation and with given precision?

后端 未结 14 2195
臣服心动
臣服心动 2020-11-22 04:28

I\'m curious, whether there is any way to print formatted numpy.arrays, e.g., in a way similar to this:

x = 1.23456
print \'%.3f\' % x
         


        
14条回答
  •  故里飘歌
    2020-11-22 04:38

    The gem that makes it all too easy to obtain the result as a string (in today's numpy versions) is hidden in denis answer: np.array2string

    >>> import numpy as np
    >>> x=np.random.random(10)
    >>> np.array2string(x, formatter={'float_kind':'{0:.3f}'.format})
    '[0.599 0.847 0.513 0.155 0.844 0.753 0.920 0.797 0.427 0.420]'
    

提交回复
热议问题