在显示格式中为dtype中的浮点数设置精度

时间:2019-12-18 04:45:03

标签: python floating-point dtype

我有一个csv文件,如下所示:

72,35,0,33.6,6,148,0.627,50,1
66,29,0,26.6,1,85,0.351,31,0
64,0,0,23.3,8,183,0.672,32,1
...

我正在尝试使用以下代码行从此文件中读取值:

dataset = np.genfromtxt(filename, delimiter=',', dtype=float)

我的问题是现在我的数据框看起来像这样(科学记号):

7.200000000000000000e+01,3.500000000000000000e+01,0.000000000000000000e+00,3.360000000000000142e+01,6.000000000000000000e+00,1.480000000000000000e+02,6.270000000000000018e-01,5.000000000000000000e+01,1.000000000000000000e+00
6.600000000000000000e+01,2.900000000000000000e+01,0.000000000000000000e+00,2.660000000000000142e+01,1.000000000000000000e+00,8.500000000000000000e+01,3.509999999999999787e-01,3.100000000000000000e+01,0.000000000000000000e+00
6.400000000000000000e+01,0.000000000000000000e+00,0.000000000000000000e+00,2.330000000000000071e+01,8.000000000000000000e+00,1.830000000000000000e+02,6.720000000000000417e-01,3.200000000000000000e+01,1.000000000000000000e+00

无论如何,我仍然可以在代码中设置dtype的值以避免科学计数法,同时仍将数据读取为浮点数吗?

1 个答案:

答案 0 :(得分:0)

一些潜在的建议:

  1. 使用np.loadtxt代替np.genfromtxt

    dataset = np.loadtxt(filename,delimiter =',fmt =“%2.3f”,delimiter =“,”)

  2. 导入python的“十进制”模块以提高精度,并将科学计数法字符串转换为浮点十进制值。