从嵌套列表中创build数组时,禁止在Numpy中使用科学表示法

我有一个嵌套的列表,如下所示:

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

然后导入Numpy,并设置打印选项(suppress=True) 。 当我创build一个数组时:

 my_array = numpy.array(my_list) 

我不能为了我的生活压制科学记数法:

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

如果我直接创build一个简单的数组:

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

我没有问题,它打印如下:

 [ 1.5 4.65 7.845] 

有谁知道我的问题是什么?

我想你需要的是np.set_printoptions(suppress=True) ,详情请看这里: http : np.set_printoptions(suppress=True)

对于SciPy.org numpy文档,其中包括所有的function参数(压制不详细在上面的链接),请参阅: https : //docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html

对于一维和二维数组,您可以使用np.savetxt使用特定的格式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 

v1.3中的numpy.set_printoptions或numpy.array2string的选项非常笨重和有限(例如没有办法为大数字压缩科学记数法)。 看起来这将改变与未来的版本,numpy.set_printoptions(formatter = ..)和numpy.array2string(style = ..)。

我不太明白你的目标,但是如果你想让输出显示在非科学的浮标上,你可以做

 map(list, my_array) 

如果这是你的值的问题,你可以在创build时指定数据types

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

我的猜测是,numpy模块猜测你的数字最好用科学记数法显示,因为你有这么多的数字。

一个演示Python 2.7强制禁止打印numpy ndarrays中的所有指数表示法,如果没有,或者有关它的。

通过suppress=Trueset_printoptions工作在第一…,它抑制了小数字的指数表示,如下所示:

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

但是如果你input一个大于8个字符的数字,那么numpy的力量就会再次呈指数forms了! 有没有搞错:

 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] 

这是因为在select把你的数字减半,在它所具有的空间中歪曲它,或者强制使用指数符号,select了后者。

set_printoptions(formatter = …)来拯救。 告诉set_printoptions格式化程序只打印一个空的float:

 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] 

我们已经强制压制了指数表示法,但它很丑,而不是风格,所以指定额外的格式化选项:

 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] 

强制压制所有指数概念的缺点在于,如果你的ndarray在无穷远处有一个巨大的浮点值并且打印出来,那么你将会面对一个充满数字的页面。

你可以写一个函数,把科学记数法转换成常规的,比如

 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