Numpy loadtxt rounds numbers

I am using the numpy loadtxt function to read in a large dataset. The data seems to be rounded. for example: the number in the text file is -3.79000000000005E + 01, but numpy reads the number as -37.9. I set dypte to np.float64 in a loadtxt call. Is there a way to keep the accuracy of the original data file?

+5
source share
1 answer

loadtxtDon't round a number. What you see is the way NumPy chooses to print an array:

In [80]: import numpy as np

In [81]: x = np.loadtxt('test.dat', dtype = np.float64)

In [82]: print(x)
-37.9

The actual value is np.float64 closest to the value entered.

In [83]: x
Out[83]: array(-37.9000000000005)

Or, in the more likely case, if you have a higher dimensional array,

In [2]: x = np.loadtxt('test.dat', dtype = np.float64)

If reprof xlooks truncated:

In [3]: x
Out[3]: array([-37.9, -37.9])

np.set_printoptions :

In [4]: np.get_printoptions()
Out[4]: 
{'edgeitems': 3,
 'infstr': 'inf',
 'linewidth': 75,
 'nanstr': 'nan',
 'precision': 8,
 'suppress': False,
 'threshold': 1000}

In [5]: np.set_printoptions(precision = 17)

In [6]: x
Out[6]: array([-37.90000000000050306, -37.90000000000050306])

( @mgilson .)

+5

All Articles