简体   繁体   中英

Inconsistent Precision printing numpy arrays

I have these two arrays being printed.

array([[-7.00e+00, -9.00e+00,  0.00e+00,  1.60e+01,  0.00e+00, -9.00e+00, -7.00e+00, -1.20e+01],
       [-6.00e+00, -1.70e+01,  1.11e-15,  2.00e+01,  1.33e-15, -1.70e+01, -6.00e+00,  2.00e+00],
       [-3.00e+00, -1.90e+01,  1.00e+01,  2.80e+01,  1.00e+01, -1.90e+01, -3.00e+00,  2.40e+01],
       [ 6.00e+00,  0.00e+00,  1.40e+01,  1.60e+01,  1.40e+01,  1.78e-15,  6.00e+00,  2.40e+01],
       [-3.00e+00, -1.90e+01,  1.00e+01,  2.80e+01,  1.00e+01, -1.90e+01, -3.00e+00,  2.40e+01],
       [-6.00e+00, -1.70e+01, -1.11e-15,  2.00e+01,  4.44e-16, -1.70e+01, -6.00e+00,  2.00e+00],
       [-7.00e+00, -9.00e+00,  0.00e+00,  1.60e+01,  8.88e-16, -9.00e+00, -7.00e+00, -1.20e+01],
       [-1.40e+01, -2.20e+01,  6.00e+00,  3.20e+01,  6.00e+00, -2.20e+01, -1.40e+01, -4.00e+00]])
array([[ -9.96, -29.6 ,  -7.68,  47.32],
       [-11.7 , -17.86, -10.47,  25.97],
       [ 28.45, -22.42,  -9.69,  46.16],
       [ 42.94,  -2.22,  11.06,  40.59]])

When logging their type, they are both float64. I wonder why one as scientific notation and not the other. Also, how to set it to always have the second notation.

I have this on the top of my script

np.set_printoptions(linewidth=240, precision=2)

But apparently it is not holding for all cases.

Some of the elements of the first array are tiny, like this one:

-1.11e-15

The e-15 means "times 10 to the power of -15". That'd take a ton of digits to display in ordinary positional notation, so NumPy switches to scientific notation. For layout consistency, this applies to the whole array.

You can have NumPy's array printing logic instead treat these values as (appropriately signed) zero by setting the suppress print option to True :

numpy.set_printoptions(suppress=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM