arrays - np.around and np.round not actually rounding -


i'm writing simple code in python 2.7 change couple long files have text files can scroll through them in text reader.

however, found out numpy.array in file has long floats end in unneeded scientific notation. try , use numpy.around or numpy.round change these have 2 places after decimal doesn't change anything. here code:

import h5py import sys tkinter import tk tkfiledialog import askopenfilename import numpy np  sys.stdout.write( 'please pick file window\n') filename = askopenfilename() # show "open" dialog box , return path selected file sys.stdout.write(filename) f = h5py.file(filename, 'r') dataset = f['/dcoor'][:]   newname = raw_input('new file name ') print type(dataset[0][0]) dataset = np.asarray(dataset) dataset = dataset.astype(float) print type(dataset[0][0]) print '\ndataset before rounding: \n', dataset dataset = np.around(dataset, decimals = 2) print '\ndataset after rounding: \n', dataset np.savetxt(newname,dataset) 

i not error messages , output this:

new file name test4 <type 'numpy.float32'> <type 'numpy.float64'>  dataset before rounding:  [[  1.48999996e+01   1.07949997e+02   1.80000007e-01   3.59000000e+02     0.00000000e+00]  [  1.60100002e+01   1.07489998e+02   3.89999986e-01   3.98000000e+02     0.00000000e+00]  [  1.86700001e+01   1.07669998e+02   5.89999974e-01   4.26000000e+02     0.00000000e+00]  ...,   [  2.78700008e+01   2.75200005e+01   2.99973999e+03   4.15000000e+02     0.00000000e+00]  [  2.60499992e+01   2.72800007e+01   2.99991992e+03   4.10000000e+02     0.00000000e+00]  [  2.56599998e+01   2.85400009e+01   3.00009009e+03   4.37500000e+02     0.00000000e+00]]  dataset after rounding:  [[  1.49000000e+01   1.07950000e+02   1.80000000e-01   3.59000000e+02     0.00000000e+00]  [  1.60100000e+01   1.07490000e+02   3.90000000e-01   3.98000000e+02     0.00000000e+00]  [  1.86700000e+01   1.07670000e+02   5.90000000e-01   4.26000000e+02     0.00000000e+00] 

which odd since appears round numbers not others, , keeps trailing zeros well. converted original array because thought might make difference did not. problem array's long? each 1 16,000 rows. original array saved in hdf5 file keeps original format? can't go , retest mice work if that's case i'm rather sol. thank help.

the numbers are being rounded. reason why aren't precisely 2 decimal places because ieee 754 floating point numbers have rounding errors. since can't represent floating point numbers (with limited size) in given base (base 2 in case), there implicit precision problems.

think numbers 2/3 or 5/7. can't represent them in base 10.

however, i'm not sure why care fact way numpy visually represents floats repr uses scientific notation. when want write them out can use loop on array , specify precision when writing:

for row in dataset:     elem in row:         somefile.write("%.2f" % (elem,)) 

this ensure 2 decimal places written (and round in way you're trying to). it's important note when load files, still have same ieee 754 drawbacks.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -