Suppress scientific notation in Numpy when creating an array from a nested list

I have a nested list that looks like this:

my_list = [[3.74, 5162, 13683628846.64, 12783387559.86, 1.81], [9.55, 116, 189688622.37, 260332262.0, 1.97], [2.2, 768, 6004865.13, 5759960.98, 1.21], [3.74, 4062, 3263822121.39, 3066869087.9, 1.93], [1.91, 474, 44555062.72, 44555062.72, 0.41], [5.8, 5006, 8254968918.1, 7446788272.74, 3.25], [4.5, 7887, 30078971595.46, 27814989471.31, 2.18], [7.03, 116, 66252511.46, 81109291.0, 1.56], [6.52, 116, 47674230.76, 57686991.0, 1.43], [1.85, 623, 3002631.96, 2899484.08, 0.64], [13.76, 1227, 1737874137.5, 1446511574.32, 4.32], [13.76, 1227, 1737874137.5, 1446511574.32, 4.32]] 

Then I import Numpy and set the print options (suppress=True) . When I create an array:

 my_array = numpy.array(my_list) 

For life, I cannot suppress the scientific notation:

 [[ 3.74000000e+00 5.16200000e+03 1.36836288e+10 1.27833876e+10 1.81000000e+00] [ 9.55000000e+00 1.16000000e+02 1.89688622e+08 2.60332262e+08 1.97000000e+00] [ 2.20000000e+00 7.68000000e+02 6.00486513e+06 5.75996098e+06 1.21000000e+00] [ 3.74000000e+00 4.06200000e+03 3.26382212e+09 3.06686909e+09 1.93000000e+00] [ 1.91000000e+00 4.74000000e+02 4.45550627e+07 4.45550627e+07 4.10000000e-01] [ 5.80000000e+00 5.00600000e+03 8.25496892e+09 7.44678827e+09 3.25000000e+00] [ 4.50000000e+00 7.88700000e+03 3.00789716e+10 2.78149895e+10 2.18000000e+00] [ 7.03000000e+00 1.16000000e+02 6.62525115e+07 8.11092910e+07 1.56000000e+00] [ 6.52000000e+00 1.16000000e+02 4.76742308e+07 5.76869910e+07 1.43000000e+00] [ 1.85000000e+00 6.23000000e+02 3.00263196e+06 2.89948408e+06 6.40000000e-01] [ 1.37600000e+01 1.22700000e+03 1.73787414e+09 1.44651157e+09 4.32000000e+00] [ 1.37600000e+01 1.22700000e+03 1.73787414e+09 1.44651157e+09 4.32000000e+00]] 

If I create a simple array directly:

 new_array = numpy.array([1.5, 4.65, 7.845]) 

I have no problem and it prints like this:

 [ 1.5 4.65 7.845] 

Does anyone know what my problem is?

+56
python numpy
Mar 19 2018-12-12T00:
source share
5 answers

I assume you need np.set_printoptions(suppress=True) , see here for details: http://pythonquirks.blogspot.fr/2009/10/controlling-printing-in-numpy.html

For SciPy.org numpy documentation, which includes all functional parameters (suppression is not described in the link above), see here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html

+112
Jun 01 2018-12-12T00:
source share

for 1D and 2D arrays you can use np.savetxt to print using a specific format string:

 >>> import sys >>> x = numpy.arange(20).reshape((4,5)) >>> numpy.savetxt(sys.stdout, x, '%5.2f') 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 

Your options with numpy.set_printoptions or numpy.array2string in version 1.3 are rather awkward and limited (for example, there is no way to suppress scientific notation for large numbers). It looks like this will change with future versions, with numpy.set_printoptions (formatter = ..) and numpy.array2string (style = ..).

+12
Jul 17 '12 at 18:40
source share

I donโ€™t quite understand your goal, but if you want the result to appear in unscientific floats, you could do

 map(list, my_array) 

If this is a problem rooting your values, you can specify the data type when creating

 my_array = numpy.array(my_list, dtype=numpy.float64) 

I guess the numpy module guesses that your numbers are best displayed in scientific notation, since you have such large numbers.

+1
Mar 19 '12 at 21:05
source share

Python 2.7 Demo Force suppression of all exponential notations when printing ndarrays numpy, no if's and / or butts.

Passing suppress=True to set_printoptions works first ..., it suppresses exponential notation in small amounts, for example:

 import numpy as np np.set_printoptions(suppress=True) #prevent numpy exponential #notation on print, default False # tiny med large a = np.array([1.01e-5, 22, 1.2345678e7]) #notice how index 2 is 8 #digits wide print(a) #prints [ 0.0000101 22. 12345678. ] 

But then, if you go through more than 8 characters, then numpy forces exponential notation again! What the hell:

 np.set_printoptions(suppress=True) a = np.array([1.01e-5, 22, 1.2345678e10]) #notice how index 2 is 10 #digits wide, too wide! #exponential notation where we've told it not to! print(a) #prints [1.01000000e-005 2.20000000e+001 1.23456780e+10] 

This is because numpy is torn between choosing to chop your number in half, distorting it in the space that it has, or forced exponential notation, it chooses the latter.

set_printoptions (formatter = ...) for salvation. Say setform_printoptions formatter to just print a bare floating file:

 np.set_printoptions(suppress=True, formatter={'float_kind':'{:f}'.format}) a = np.array([1.01e-5, 22, 1.2345678e30]) #notice how index 2 is 30 #digits wide. #Ok good, no exponential notation in the large numbers: print(a) #prints [0.000010 22.000000 1234567799999999979944197226496.000000] 

We force suppression of exponential notation, but it is ugly and not in style, so specify additional formatting options:

 np.set_printoptions(suppress=True, formatter={'float_kind':'{:0.2f}'.format}) #float, 2 units #precision right, 0 on left a = np.array([1.01e-5, 22, 1.2345678e30]) #notice how index 2 is 30 #digits wide print(a) #prints [0.00 22.00 1234567799999999979944197226496.00] 

The disadvantage to forcibly suppressing the entire exponential notion in ndarrays is that if your ndarray gets a huge float near infinity in it and you print it, you will be blown into the face with a page full of numbers,

+1
Oct 08 '17 at 19:36 on
source share

You can write a function that converts scientific notation to regular, something like

 def sc2std(x): s = str(x) if 'e' in s: num,ex = s.split('e') if '-' in num: negprefix = '-' else: negprefix = '' num = num.replace('-','') if '.' in num: dotlocation = num.index('.') else: dotlocation = len(num) newdotlocation = dotlocation + int(ex) num = num.replace('.','') if (newdotlocation < 1): return negprefix+'0.'+'0'*(-newdotlocation)+num if (newdotlocation > len(num)): return negprefix+ num + '0'*(newdotlocation - len(num))+'.0' return negprefix + num[:newdotlocation] + '.' + num[newdotlocation:] else: return s 
0
Jun 29 '16 at 20:52
source share



All Articles