python,指数和十进制浮点数

时间:2012-09-25 08:39:31

标签: python numpy floating-point exponential

我想使用numpy.savetxt()将带有浮点数的数组保存到文件中。 浮点数必须是小数(非指数!)

代码如下:

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

wparams是我的函数permfitfuncLD()的一些参数,它本身很复杂。 输出是:

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

我的问题是,值必须是浮点数(即第一个值应为0.18787)。 我不明白为什么数组arr中的数字具有指数形式。

我真的很感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

这些花车。这只是显示浮动的另一种方式。 Numpy数组默认以指数格式打印浮点数。如果您需要另一种格式将数据存储在文本文件中,则必须使用numpy.save.savetxt或使用python格式化函数自行完成。

但是,您应该意识到与科学格式相比,您将失去精确度。原因是科学格式可以代表相同数量的非常小的数字,因为它可以代表非常高的数字。你不能用十进制表示法做到这一点。

答案 1 :(得分:1)

如果您对在NumPy中打印花车阵列感兴趣,可以:

  • 使用np.savetxt功能的fmt参数。例如,如果要将{3}个浮点列打印为"%.5f",用空格分隔,则使用fmt="%.5f %.5f ".5f"
  • 如果要更改屏幕上打印NumPy阵列的方式,可以使用np.set_printoptions(precision=5)功能,例如使用dtype="|S10"

在字符串数组中转换float数组还有最后一招:只需使用类似wav.astype("|S10")的字符串dtype(例如,"|S10"。这需要一些小问题才能找到合适的字符串你想要的字符串大小({{1}}意味着'最多10个字符),就像你打印更大的东西一样,它会被截断。我不会在现实生活中提出建议,但作为练习它是无害的。