【问题标题】:Avoiding exponential values in loadtxt [duplicate]避免 loadtxt 中的指数值
【发布时间】:2014-07-31 13:56:56
【问题描述】:

我有一个 csv 文件,其标题如下:

鉴于此test.csv 文件:

"A","B","C","D","E","F","timestamp"
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486

如果,我使用load.txt,那么我会得到一个包含exponential 值的3 行7 列数组。

r1 = numpy.loadtxt(open("test.csv","rb"),delimiter=",",skiprows=1)

我明白了

 [[  6.11882430e+02   9.08956010e+03   5.13300000e+03   8.64075140e+02
     1.71537476e+03   7.65227770e+02   1.29111196e+12]
  [  6.11882430e+02   9.08956010e+03   5.13300000e+03   8.64075140e+02
     1.71537476e+03   7.65227770e+02   1.29111311e+12]
  [  6.11882430e+02   9.08956010e+03   5.13300000e+03   8.64075140e+02
     1.71537476e+03   7.65227770e+02   1.29112065e+12]]

为了避免exponential,我使用了以下代码,但它仍然给出了相同的指数值。我的代码避免指数:

 r1 = np.loadtxt(open("test.csv","rb"),delimiter=",", dtype=np.float64, skiprows=1)

在创建 numpy 矩阵时有什么方法可以删除 exponential 吗?我知道我可以稍后使用 numpy.savetxt(sys.stdout, r1, '%5.2f') 删除这些值,但我希望它在创建矩阵时而不是在创建之后。

【问题讨论】:

  • 611.88243 is 6.11882430e+02,考虑到与浮点运算相关的问题)。您不想读入值吗?否则,您在寻找什么样的结果?另外,您想对已经采用指数表示法 (1.291111964948E12) 的最后一列输入做什么?
  • 为什么重要?除了显示方式之外还有什么不同吗?
  • @JoshuaTaylor 查看我的编辑,csv 文件中不会有任何指数值。
  • @RobWatts 是的,我希望我的矩阵看起来很简单
  • @user2481422 那么这只是你如何打印出来的问题 - 你的矩阵的内容没有任何问题。 stackoverflow.com/questions/2891790/… 回答如何做到这一点。

标签: python csv numpy matrix exponential


【解决方案1】:

我希望问题上的 cmets 清楚地表明这纯粹是一个格式问题。在 cmets 中还指出,@unutbu 在这里给出了对 numpy 数组的一些格式化选项的一个很好的解释:How to pretty-printing a numpy.array without scientific notation and with given precision?

该答案中未显示的一个选项是使用formatter 参数到np.set_printoptions。在 numpy 版本 1.7.0 中,该参数已添加到 set_printoptions。使用 formatter 参数,您可以控制 numpy 如何打印数组元素。下面是一个使用该参数控制浮点数格式的示例。

下面是使用默认设置打印a 的方式:

In [30]: a
Out[30]: 
array([[  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,
          8.64075140e+02,   1.71537476e+03,   7.65227770e+02,
          1.29111196e+12],
       [  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,
          8.64075140e+02,   1.71537476e+03,   7.65227770e+02,
          1.29111311e+12],
       [  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,
          8.64075140e+02,   1.71537476e+03,   7.65227770e+02,
          1.29112065e+12]])

现在覆盖默认值,并告诉 numpy 使用 "%.5f" 格式将浮点值转换为字符串。此格式不使用科学计数法,始终显示小数点后五位。

In [31]: np.set_printoptions(formatter={'float': lambda x: "%.5f" % (x,)})

In [32]: a
Out[32]: 
array([[611.88243, 9089.56010, 5133.00000, 864.07514, 1715.37476,
        765.22777, 1291111964948.00000],
       [611.88243, 9089.56010, 5133.00000, 864.07514, 1715.37476,
        765.22777, 1291113113366.00000],
       [611.88243, 9089.56010, 5133.00000, 864.07514, 1715.37476,
        765.22777, 1291120650486.00000]])

您可以添加对rstrip 的调用以删除尾随零:

In [53]: np.set_printoptions(formatter={'float': lambda x: ("%.5f" % (x,)).rstrip('0')})

In [54]: a
Out[54]: 
array([[611.88243, 9089.5601, 5133., 864.07514, 1715.37476, 765.22777,
        1291111964948.],
       [611.88243, 9089.5601, 5133., 864.07514, 1715.37476, 765.22777,
        1291113113366.],
       [611.88243, 9089.5601, 5133., 864.07514, 1715.37476, 765.22777,
        1291120650486.]])

请注意,在上面,我在ipython 中输入了名称,它回显了它的值。以这种方式使用时,将打印对象的repr-representation。如果您明确打印它,您将获得str-representation:

In [55]: print(a)
[[611.88243 9089.5601 5133. 864.07514 1715.37476 765.22777 1291111964948.]
 [611.88243 9089.5601 5133. 864.07514 1715.37476 765.22777 1291113113366.]
 [611.88243 9089.5601 5133. 864.07514 1715.37476 765.22777 1291120650486.]]

【讨论】:

  • 你的回答给出了这个错误TypeError: set_printoptions() got an unexpected keyword argument 'formatter'
  • @user2481422:在 numpy 1.7.0 (github.com/numpy/numpy/blob/master/doc/release/…) 中添加了 formatter 参数。你用的是什么版本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2017-02-18
  • 2013-11-28
  • 1970-01-01
  • 2013-01-31
相关资源
最近更新 更多