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

后端 未结 14 2411
臣服心动
臣服心动 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:52

    You can get a subset of the np.set_printoptions functionality from the np.array_str command, which applies only to a single print statement.

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.array_str.html

    For example:

    In [27]: x = np.array([[1.1, 0.9, 1e-6]]*3)
    
    In [28]: print x
    [[  1.10000000e+00   9.00000000e-01   1.00000000e-06]
     [  1.10000000e+00   9.00000000e-01   1.00000000e-06]
     [  1.10000000e+00   9.00000000e-01   1.00000000e-06]]
    
    In [29]: print np.array_str(x, precision=2)
    [[  1.10e+00   9.00e-01   1.00e-06]
     [  1.10e+00   9.00e-01   1.00e-06]
     [  1.10e+00   9.00e-01   1.00e-06]]
    
    In [30]: print np.array_str(x, precision=2, suppress_small=True)
    [[ 1.1  0.9  0. ]
     [ 1.1  0.9  0. ]
     [ 1.1  0.9  0. ]]
    

提交回复
热议问题