Python 如何在 str(numpy.float64) 上设置精度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4844865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:49:28  来源:igfitidea点击:

How to set the precision on str(numpy.float64)?

pythonnumpyscipy

提问by Fookatchu

i need to write a couple of numpy floats to a csv-file which has additional string content. therefore i dont use savetxt etc. with numpy.set_printoptions() i can only define the print behaviour, but not the str() behaviour. i know that i miss something and it cant be that hard, but i dont find a reasonable answer on the interwebs. maybe someone can point me in the right direction. heres some example code:

我需要将几个 numpy 浮点数写入具有附加字符串内容的 csv 文件。因此,我不将 savetxt 等与 numpy.set_printoptions() 一起使用,我只能定义打印行为,而不能定义 str() 行为。我知道我错过了一些东西并且不会那么难,但我在互联网上找不到合理的答案。也许有人可以指出我正确的方向。继承人一些示例代码:

In [1]: import numpy as np
In [2]: foo = np.array([1.22334])

In [3]: foo
Out[3]: array([ 1.22334])

In [4]: foo[0]
Out[4]: 1.2233400000000001

In [5]: str(foo[0])
Out[5]: '1.22334'

In [6]: np.set_printoptions(precision=3)

In [7]: foo
Out[7]: array([ 1.223])

In [8]: foo[0]
Out[8]: 1.2233400000000001

In [9]: str(foo[0])
Out[9]: '1.22334'

How do i convert np.float to a nicely formatted string, which i can feed to file.write()?

我如何将 np.float 转换为格式良好的字符串,我可以将其提供给 file.write()?

kind regards,

亲切的问候,

fookatchu

福卡丘

采纳答案by David Heffernan

You can just use standard string formatting:

您可以只使用标准字符串格式:

>>> x = 1.2345678
>>> '%.2f' % x
'1.23'

回答by tillsten

You could use normal String formating, see: http://docs.python.org/library/string.html#formatspec

您可以使用普通的字符串格式,请参阅:http: //docs.python.org/library/string.html#formatspec

Example:

例子:

print '{:.2f}'.format(0.1234)  # '0.12'
print '{:.2e}'.format(0.1234)  # '1.23e-01'

回答by Ned Batchelder

Instead of str(foo[0]), use "%.3f" % foo[0].

而不是str(foo[0]),使用"%.3f" % foo[0]

回答by fabrica

Also you can do:

你也可以这样做:

precision = 2
str(np.round(foo[0], precision))

It had some advantages for me over the ('%.2f' % x) when I needed to do string a str(np.log(0.0)) which is neatly treated to "-inf" by numpy so you don't have to bother here.

当我需要做字符串 str(np.log(0.0)) 时,它对我来说比 ('%.2f' % x) 有一些优势,它被 numpy 巧妙地处理为“-inf”,所以你没有打扰这里。

回答by Mike T

Numpy 1.14 and later have format_float_positionaland format_float_scientificfunctions to format a floating-point scalar as a decimal string in positional or scientific notation, with control over rounding, trimming and padding. These functions offer much more control to the formatting than conventional Python string formatters.

numpy的1.14和以后有format_float_positionalformat_float_scientific功能格式化浮点标如在位置或科学记数法十进制字符串,具有过四舍五入,修整和填充控制。与传统的 Python 字符串格式化程序相比,这些函数对格式化提供了更多的控制。

import numpy as np

x = np.float64('1.2345678')
print(np.format_float_positional(x))  # 1.2345678
print(np.format_float_positional(x, precision=3))  # 1.235
print(np.format_float_positional(np.float16(x)))  # 1.234
print(np.format_float_positional(np.float16(x), unique=False, precision=8))  # 1.23437500

y = x / 1e8
print(np.format_float_scientific(y))  # 1.2345678e-08
print(np.format_float_scientific(y, precision=3, exp_digits=1))  # 1.235e-8

etc.

等等。



These advanced formatters are based on the Dragon4 algorithm; see Ryan Juckett's Printing Floating-Point Numbersto read more on the subject.

这些高级格式化程序基于 Dragon4 算法;请参阅 Ryan Juckett 的打印浮点数以阅读有关该主题的更多信息。