Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy loadtxt rounding off numbers

Tags:

python

numpy

I'm using numpy loadtxt function to read in a large set of data. The data appears to be rounded off. for example: The number in the text file is -3.79000000000005E+01 but numpy reads the number in as -37.9. I've set the dypte to np.float64 in the loadtxt call. Is there anyway to keep the precision of the original data file?

like image 891
JMD Avatar asked Feb 14 '13 23:02

JMD


People also ask

How do I round off in NumPy?

The ceil() function rounds off decimal to nearest upper integer. E.g. ceil of 3.166 is 4.

What does Loadtxt () do in NumPy?

loadtxt() function. The loadtxt() function is used to load data from a text file. Each row in the text file must have the same number of values.

How do you load a text file in Python?

To read a text file in Python, you follow these steps: First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object. Third, close the file using the file close() method.


1 Answers

loadtxt is not rounding the number. What you are seeing is the way NumPy chooses to print the 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 the np.float64 closest to the value inputted.

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

Or, in the more likely instance that you have a higher dimensional array,

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

If the repr of x looks truncated:

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

you can use np.set_printoptions to get higher precision:

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])

(Thanks to @mgilson for pointing this out.)

like image 148
unutbu Avatar answered Oct 01 '22 16:10

unutbu