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:
我需要写几个浮点数到一个csv文件,它有附加的字符串内容。因此,我不能使用numpy.set_printoptions()来使用savetxt,我只能定义打印行为,但不能定义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。浮动到一个格式良好的字符串,我可以将其提供给file.write()?
kind regards,
亲切的问候,
fookatchu
fookatchu
5 个解决方案
#1
8
You can just use standard string formatting:
您可以使用标准的字符串格式:
>>> x = 1.2345678
>>> '%.2f' % x
'1.23'
#2
6
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'
#3
3
Instead of str(foo[0])
, use "%.3f" % foo[0]
.
使用“%”代替str(foo[0])。3 f % foo[0]。
#4
2
Numpy 1.14 and later have format_float_positional
and format_float_scientific
functions 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_position和format_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 Numbers to read more on the subject.
这些高级格式器基于Dragon4算法;请参阅Ryan Juckett印刷的浮点数来了解更多关于这个主题的内容。
#5
0
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.
它比%对我有一些好处。2f' % x)当我需要执行一个str(np.log(0.0))时,它被numpy巧妙地处理为“-inf”,这样你就不用麻烦了。
#1
8
You can just use standard string formatting:
您可以使用标准的字符串格式:
>>> x = 1.2345678
>>> '%.2f' % x
'1.23'
#2
6
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'
#3
3
Instead of str(foo[0])
, use "%.3f" % foo[0]
.
使用“%”代替str(foo[0])。3 f % foo[0]。
#4
2
Numpy 1.14 and later have format_float_positional
and format_float_scientific
functions 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_position和format_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 Numbers to read more on the subject.
这些高级格式器基于Dragon4算法;请参阅Ryan Juckett印刷的浮点数来了解更多关于这个主题的内容。
#5
0
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.
它比%对我有一些好处。2f' % x)当我需要执行一个str(np.log(0.0))时,它被numpy巧妙地处理为“-inf”,这样你就不用麻烦了。