python, exponential and decimal float - python
I want to save an array with floats to at file with numpy.savetxt().
The floats have to be decimals (so non-exponential!)
The code is as follows:
wav = 1./w
wav = np.array(sorted(wav))
efittet = permfitfuncLD(params1[0], params1[1:],1./wav).real
eefittet = permfitfuncLD(params1[0], params1[1:],1./wav).imag
etoprint = [float(s) for s in ["%.5f" % i for i in efittet]]
eetoprint = [float(s) for s in ["%.5f" % i for i in eefittet]]
wavtoprint = [float(s) for s in ["%.5f" % i for i in wav]]
print "--------------------------------------------------------------------"
print "etoprint"
print etoprint
print "--------------------------------------------------------------------"
print "eetoprint"
print eetoprint
print "--------------------------------------------------------------------"
print "wavtoprint"
print wavtoprint
print "--------------------------------------------------------------------"
arr = np.array([wavtoprint,etoprint,eetoprint]).transpose()
print "txt to safe should contain: ", arr
w and params are some parameters for my function permfitfuncLD() which itself is complex.
The output is:
--------------------------------------------------------------------
etoprint
[0.50391, 0.44551, 0.37837, 0.29117, 0.19734, 0.09081, -0.03854, -0.17022, -0.325, -0.47742, -0.63656, -0.8125, -0.9728, -1.13589, -1.26684, -1.36734, -1.42736, -1.42551, -1.3545, -1.23074, -1.07199, -0.90215, -0.79518, -0.78347, -0.899, -1.14185, -1.51655, -1.87591, -2.07595, -1.89561, -1.53701, -1.56583, -2.20596, -3.38828, -4.77624, -6.39743, -8.43319, -10.62524, -13.43619, -16.55797, -20.35308, -25.49629, -31.56653, -40.21232, -51.0312, -66.09163, -90.35934, -125.69555, -188.95742]
--------------------------------------------------------------------
eetoprint
[3.51955, 3.48008, 3.44968, 3.42508, 3.41183, 3.40951, 3.4217, 3.44898, 3.49899, 3.5671, 3.6595, 3.79075, 3.94391, 4.14716, 4.3697, 4.62296, 4.92274, 5.20694, 5.49578, 5.71423, 5.85755, 5.90724, 5.8566, 5.72561, 5.58159, 5.4753, 5.48067, 5.66307, 6.04179, 6.30754, 6.06509, 5.20939, 4.3096, 3.51667, 2.98072, 2.59107, 2.28845, 2.09188, 1.9525, 1.88784, 1.89048, 1.98989, 2.20993, 2.67266, 3.45841, 4.87942, 7.80959, 13.04591, 25.27873]
--------------------------------------------------------------------
wavtoprint
[0.18787, 0.19165, 0.19527, 0.19935, 0.20327, 0.20735, 0.21196, 0.2164, 0.22142, 0.22627, 0.23134, 0.23709, 0.24265, 0.24899, 0.25514, 0.2616, 0.26897, 0.27616, 0.2844, 0.29244, 0.30096, 0.31077, 0.3204, 0.33154, 0.34253, 0.35428, 0.36794, 0.38153, 0.39742, 0.41332, 0.43054, 0.4509, 0.47147, 0.49599, 0.52099, 0.54866, 0.58214, 0.6169, 0.65956, 0.70453, 0.75608, 0.82117, 0.89206, 0.9841, 1.08769, 1.21565, 1.39322, 1.61035, 1.93745]
--------------------------------------------------------------------
txt to safe should contain: [[ 1.87870000e-01 5.03910000e-01 3.51955000e+00]
[ 1.91650000e-01 4.45510000e-01 3.48008000e+00]
[ 1.95270000e-01 3.78370000e-01 3.44968000e+00]
[ 1.99350000e-01 2.91170000e-01 3.42508000e+00]
[ 2.03270000e-01 1.97340000e-01 3.41183000e+00]
[ 2.07350000e-01 9.08100000e-02 3.40951000e+00]
[ 2.11960000e-01 -3.85400000e-02 3.42170000e+00]
[ 2.16400000e-01 -1.70220000e-01 3.44898000e+00]
[ 2.21420000e-01 -3.25000000e-01 3.49899000e+00]
[ 2.26270000e-01 -4.77420000e-01 3.56710000e+00]
[ 2.31340000e-01 -6.36560000e-01 3.65950000e+00]
[ 2.37090000e-01 -8.12500000e-01 3.79075000e+00]
[ 2.42650000e-01 -9.72800000e-01 3.94391000e+00]
[ 2.48990000e-01 -1.13589000e+00 4.14716000e+00]
[ 2.55140000e-01 -1.26684000e+00 4.36970000e+00]
[ 2.61600000e-01 -1.36734000e+00 4.62296000e+00]
[ 2.68970000e-01 -1.42736000e+00 4.92274000e+00]
[ 2.76160000e-01 -1.42551000e+00 5.20694000e+00]
[ 2.84400000e-01 -1.35450000e+00 5.49578000e+00]
[ 2.92440000e-01 -1.23074000e+00 5.71423000e+00]
[ 3.00960000e-01 -1.07199000e+00 5.85755000e+00]
[ 3.10770000e-01 -9.02150000e-01 5.90724000e+00]
[ 3.20400000e-01 -7.95180000e-01 5.85660000e+00]
[ 3.31540000e-01 -7.83470000e-01 5.72561000e+00]
[ 3.42530000e-01 -8.99000000e-01 5.58159000e+00]
[ 3.54280000e-01 -1.14185000e+00 5.47530000e+00]
[ 3.67940000e-01 -1.51655000e+00 5.48067000e+00]
[ 3.81530000e-01 -1.87591000e+00 5.66307000e+00]
[ 3.97420000e-01 -2.07595000e+00 6.04179000e+00]
[ 4.13320000e-01 -1.89561000e+00 6.30754000e+00]
[ 4.30540000e-01 -1.53701000e+00 6.06509000e+00]
[ 4.50900000e-01 -1.56583000e+00 5.20939000e+00]
[ 4.71470000e-01 -2.20596000e+00 4.30960000e+00]
[ 4.95990000e-01 -3.38828000e+00 3.51667000e+00]
[ 5.20990000e-01 -4.77624000e+00 2.98072000e+00]
[ 5.48660000e-01 -6.39743000e+00 2.59107000e+00]
[ 5.82140000e-01 -8.43319000e+00 2.28845000e+00]
[ 6.16900000e-01 -1.06252400e+01 2.09188000e+00]
[ 6.59560000e-01 -1.34361900e+01 1.95250000e+00]
[ 7.04530000e-01 -1.65579700e+01 1.88784000e+00]
[ 7.56080000e-01 -2.03530800e+01 1.89048000e+00]
[ 8.21170000e-01 -2.54962900e+01 1.98989000e+00]
[ 8.92060000e-01 -3.15665300e+01 2.20993000e+00]
[ 9.84100000e-01 -4.02123200e+01 2.67266000e+00]
[ 1.08769000e+00 -5.10312000e+01 3.45841000e+00]
[ 1.21565000e+00 -6.60916300e+01 4.87942000e+00]
[ 1.39322000e+00 -9.03593400e+01 7.80959000e+00]
[ 1.61035000e+00 -1.25695550e+02 1.30459100e+01]
[ 1.93745000e+00 -1.88957420e+02 2.52787300e+01]]
My Problem is, that the values have to be floats (i.e the first one should be 0.18787).
I don't understand, why the numbers in array arr have exponential form.
I really appreciate your help!
These are floats. It is just another way to display floats. Numpy arrays print the floats in exponential format by default. If you need another format for storing the data in a text file, you have to use numpy.save.savetxt or do that yourself using the python formatting functions.
However, you should be aware that you're going to loose precision compared to the scientific format. The reason is that the scientific format can represent very small numbers in the same amount of digits as it can represent very high numbers. You cannot do that with the decimal representation.
If you're interested in printing arrays of floats in NumPy, you can:
Use the fmt parameter of the np.savetxt function. For example, if you want to print 3 columns of floats as "%.5f", separated by a space, you'd use fmt="%.5f %.5f ".5f".
If you want to change the way NumPy arrays are printed on screen, you can play with the np.set_printoptions function, using for example np.set_printoptions(precision=5).
There's yet a last trick to transform an array of float in an array of strings: just use a string dtype like dtype="|S10" (eg, wav.astype("|S10"). That'll require some fiddling to find the proper size of string you want ("|S10" means 'up to 10 characters), as if you print something larger it'll get truncated. I wouldn't advise it in real life, but as an exercise it's harmless.
Related
unexpected results with np.hstack and np.c_ with regard to decimal points
I have a matrix (3x3) of matrix = [[-8.51871377e-01 -5.22712676e-01 3.29641828e-02] [ 5.22321761e-01 -8.52508933e-01 -2.02118648e-02] [ 3.86672578e-02 -6.20947349e-10 9.99252295e-01]] and I have a vector (3x1) of vector = [[0.28407955] [0.681005 ] [4.12974119]] When I concatenate the matrix with the vector via np.hstack or np.c_ then I get the following concatenation: concat = [[-8.51871377e-01 -5.22712676e-01 3.29641828e-02 2.84079552e-01] [ 5.22321761e-01 -8.52508933e-01 -2.02118648e-02 6.81005001e-01] [ 3.86672578e-02 -6.20947349e-10 9.99252295e-01 4.12974119e+00]] and not as expected: concat = [[-8.51871377e-01 -5.22712676e-01 3.29641828e-02 0.28407955...] [ 5.22321761e-01 -8.52508933e-01 -2.02118648e-02 0.681005...] [ 3.86672578e-02 -6.20947349e-10 9.99252295e-01 4.12974119e+00]] On the very right side of the concatenation it seems that the 0.'s are ignored in 0.28407955 and 0.681005 which means that the decimal point is shifted one place to the right and this leads to 2.84079552e-01 and 6.81005001e-01. Why is this happening and how can I get my expected results?
numpy vs pytorch precision
I've a numpy matrix a defined as: >>> a >>> array([[ 1.920941165 , 0.9518795607, 1.5358781432], [-0.2418292026, 0.0851087409, -0.2760766872], [-0.4161812806, 0.7409229185, -0.3248560283], [-0.3439163186, 1.4052927665, -1.612850871 ], [ 1.5810794171, 1.1820622504, 1.8063415367]]) If I typecast it to float32, it gives: >>> a.astype(np.float32) >>> array([[ 1.9209411 , 0.95187956, 1.5358782 ], [-0.2418292 , 0.08510874, -0.27607667], [-0.41618127, 0.7409229 , -0.32485604], [-0.34391633, 1.4052927 , -1.6128509 ], [ 1.5810794 , 1.1820623 , 1.8063415 ]], dtype=float32) When I convert original a matrix to a tensor, I get: >>> torch.tensor(a) >>> tensor([[ 1.9209411650, 0.9518795607, 1.5358781432], [-0.2418292026, 0.0851087409, -0.2760766872], [-0.4161812806, 0.7409229185, -0.3248560283], [-0.3439163186, 1.4052927665, -1.6128508710], [ 1.5810794171, 1.1820622504, 1.8063415367]], dtype=torch.float64) which looks correct as it retains original values from matrix a. But when I convert float32-typecasted matrix to a tensor, I get different floating point numbers. >>> torch.tensor(a.astype(np.float32)) >>> tensor([[ 1.9209411144, 0.9518795609, 1.5358781815], [-0.2418292016, 0.0851087421, -0.2760766745], [-0.4161812663, 0.7409229279, -0.3248560429], [-0.3439163268, 1.4052927494, -1.6128509045], [ 1.5810793638, 1.1820622683, 1.8063415289]]) Why can't the second tensor(tensor of type-casted matrix) be equal to the second matrix(type-casted one) provided above.
float32 has 24fraction bit (7.2 decimal point in decimal), what you see after that is not meaningful. ex: 1.920941165 (9 point). this means if you want to retain all points you shoult represent as 64 float. however when you convert to 32, either in numpy or torch they should be same values, it is only printing is different. torch print till the number of floating that you have set, while numpy truncate only till valid points. for example: import numpy as np np.set_printoptions(precision=10) a = np.array([1.920941165],dtype=np.float32) array([1.9209411], dtype=float32)** t = torch.tensor(a , dtype=torch.float32) tensor([1.9209411144]) however if you look into underlying memory of both (one uint32) are the same: np.ndarray(1, dtype=np.uint32,buffer=a ) array([1073078630], dtype=uint32) import ctypes ptr = ctypes.cast(t.data_ptr(), ctypes.POINTER(ctypes.c_uint32)) ptr[0] 1073078630
python csv writer writes array in scientific notation
I have a matrix of float numbers which i'm trying to write into a csv file, however csv writer writes it in scientific notation, i would like to leave the numbers in the original notation, i tried adding "%.2f" % but that resulted in the following error: "TypeError: only length-1 arrays can be converted to Python scalars" for item in string_color_Values: avg_color_values = [ float(item) for item in string_color_Values] array1t = np.array(avg_color_values) np.savetxt("test.csv", array1t, delimiter=",") original: [ 0.5258 1. ] [ 0.528 1. ] [ 0.5486 1. ] [ 0.5545 1. ] [ 0.732 1. ] [ 0.7872 1. ] [ 1. 1. ]] Csv which i obtain: 1.2270000000035e-01,1.0000000000e+00 2.7639999999790e-01,1.0000000000e+00 etc..
You need to have fmt="%g" in the call to np.savetxt. That will write the numbers without trailing zeroes. Alternatively if you want "cleanly" formatted nubmers you could use e.g. fmt="%.4f" (or any other format that takes your fancy). import numpy as np avg_color_Values = np.random.random((10,3)) array1t = np.array(avg_color_Values) np.savetxt("test.csv", array1t, delimiter=",", fmt = "%g") By default np.savetxt defaults to a format of %.18e, which will allows float64s to be written without loss of precicion.
Using numpy's function loadtxt() to read a two column file then using polyfit() to return an array
afile is a given file, and z the degree of the polynomial. Been breaking my head over this for a while, frustrating how I'm basically given zero instructions on how to proceed. This is what I thought it should be like: import numpy as np def newfile(afile,z): x,y = np.loadtxt(afile) d= np.polyfit(x,y,z) return d I have attempted to do it as data = np.loadtxt(afile) x = data[0:] by printing "data" I'm given this format: [[ 2. 888.8425] [ 6. 888.975 ] [ 14. 888.1026] [ 17. 888.2071] [ 23. 886.0479] [ 26. 883.3316] [ 48. 877.04 ] [ 99. 854.3665]] By printing "x" in this case just gives me the whole list (I'm thinking the issue lies in the lack of comma). In this case I'd want x to be an array of the left column.
I suppose you are getting an error when unpacking in this statement: x,y = np.loadtxt(afile) you should replace it for this: x, y = zip(*np.loadtxt(afile)) the rest should work
How to force a ndarray show in normal way instead of scientific notation?
I'm trying to print a ndarray on the screen. But python always shows it in scientific notation, which I don't like. For a scalar we can use >>> print '%2.4f' %(7.47212470e-01) 0.7472 But how to do that for a numpy.ndarray like this : [[ 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 method .astype(float) does not change the result, and .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 simply want the 0.7472 0.3717 etc.
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 precision used by default and turn off reporting 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]
I don't know about the numpy arrays but I was simply facing the same problem while doing a project in Python. Take a look at the Decimal class provided http://docs.python.org/library/decimal.html . I don't know if it's provided in numpy though.