Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a ndarray show in normal way instead of scientific notation?

I'm trying to print a ndarray on the screen. But python always shows it in scientific notation, which I don't like. For a scalar we can use

>>> print '%2.4f' %(7.47212470e-01)
0.7472

But how to do that for a numpy.ndarray like this :

[[  7.47212470e-01   3.71730070e-01   1.16736538e-01   1.22172891e-02]
 [  2.79279640e+00   1.31147152e+00   7.43946656e-02   3.08162255e-02]
 [  6.93657970e+00   3.14008688e+00   1.02851599e-01   3.96611266e-02]
 [  8.49295040e+00   3.94730094e+00   8.99398479e-02   7.60969188e-02]
 [  2.01849250e+01   8.62584092e+00   8.75722302e-02   6.17109672e-02]
 [  2.22570710e+01   1.00291292e+01   1.20918359e-01   1.07250131e-01]
 [  2.82496660e+01   1.27882133e+01   1.08438172e-01   1.58723714e-01]
 [  5.89170270e+01   2.55268510e+01   1.31990966e-01   1.61599514e-01]]

The method .astype(float) does not change the result, and .round(4) returns:

[[  7.47200000e-01   3.71700000e-01   1.16700000e-01   1.22000000e-02]
 [  2.79280000e+00   1.31150000e+00   7.44000000e-02   3.08000000e-02]
 [  6.93660000e+00   3.14010000e+00   1.02900000e-01   3.97000000e-02]
 [  8.49300000e+00   3.94730000e+00   8.99000000e-02   7.61000000e-02]
 [  2.01849000e+01   8.62580000e+00   8.76000000e-02   6.17000000e-02]
 [  2.22571000e+01   1.00291000e+01   1.20900000e-01   1.07300000e-01]
 [  2.82497000e+01   1.27882000e+01   1.08400000e-01   1.58700000e-01]
 [  5.89170000e+01   2.55269000e+01   1.32000000e-01   1.61600000e-01]]

I just simply want the 0.7472 0.3717 etc.

like image 545
gerry Avatar asked Dec 28 '22 05:12

gerry


1 Answers

The numpy.set_string_function function can be used to change the string representation of arrays.

You can also use numpy.set_print_options to change the precision used by default and turn off reporting of small numbers in scientific notation.

From the examples for set_print_options:

>>> np.set_printoptions(precision=4)
>>> print np.array([1.123456789])
[ 1.1235]
like image 75
ihuston Avatar answered Apr 30 '23 23:04

ihuston