How to make ndarray show in the usual way instead of scientific notation?

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

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

But how to do it for numpy.ndarray as follows:

 [[ 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 .astype (float) method does not change the result, but .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 want 0.7472 0.3717 etc.

+4
source share
2 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 default precision and disable the transmission 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] 
+10
source

I don't know about numpy arrays, but when running a project in Python, I ran into the same problem.

Take a look at the Decimal class provided http://docs.python.org/library/decimal.html .

I don't know if it was provided in numpy though.

0
source

All Articles