Python 在 numpy 数组中格式化浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21008858/
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
Formatting floats in a numpy array
提问by Kaly
If I have a numpy array like this:
如果我有一个像这样的 numpy 数组:
[2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01]
how can I move the decimal point and format the numbers so I end up with a numpy array like this:
我怎样才能移动小数点并格式化数字,所以我最终得到一个像这样的 numpy 数组:
[21.53, 8.13, 3.97, 10.08]
np.around(a, decimals=2)only gives me [2.15300000e+01, 8.13000000e+00, 3.97000000e+00, 1.00800000e+01]Which I don't want and I haven't found another way to do it.
np.around(a, decimals=2)只给我[2.15300000e+01, 8.13000000e+00, 3.97000000e+00, 1.00800000e+01]我不想要的,我还没有找到另一种方法来做到这一点。
采纳答案by ali_m
In order to make numpy displayfloat arrays in an arbitrary format, you can define a custom function that takes a float value as its input and returns a formatted string:
为了使 numpy以任意格式显示浮点数组,您可以定义一个自定义函数,该函数将浮点值作为其输入并返回一个格式化的字符串:
In [1]: float_formatter = "{:.2f}".format
The fhere means fixed-point format (not 'scientific'), and the .2means two decimal places (you can read more about string formatting here).
在f这里固定点格式(不是“科学”),以及.2手段小数点后两位(你可以阅读更多关于字符串格式化这里)。
Let's test it out with a float value:
让我们用一个浮点值来测试一下:
In [2]: float_formatter(1.234567E3)
Out[2]: '1234.57'
To make numpy print all float arrays this way, you can pass the formatter=argument to np.set_printoptions:
要使 numpy 以这种方式打印所有浮点数组,您可以将formatter=参数传递给np.set_printoptions:
In [3]: np.set_printoptions(formatter={'float_kind':float_formatter})
Now numpy will print all float arrays this way:
现在 numpy 将以这种方式打印所有浮点数组:
In [4]: np.random.randn(5) * 10
Out[4]: array([5.25, 3.91, 0.04, -1.53, 6.68]
Note that this only affects numpy arrays, not scalars:
请注意,这仅影响 numpy 数组,而不影响标量:
In [5]: np.pi
Out[5]: 3.141592653589793
It also won't affect non-floats, complex floats etc - you will need to define separate formatters for other scalar types.
它也不会影响非浮点数、复杂浮点数等 - 您需要为其他标量类型定义单独的格式化程序。
You should also be aware that this onlyaffects how numpy displays float values - the actual values that will be used in computations will retain their original precision.
您还应该注意,这只会影响 numpy 显示浮点值的方式 - 将在计算中使用的实际值将保留其原始精度。
For example:
例如:
In [6]: a = np.array([1E-9])
In [7]: a
Out[7]: array([0.00])
In [8]: a == 0
Out[8]: array([False], dtype=bool)
numpy prints aas if it were equal to 0, but it is not - it still equals 1E-9.
numpy 打印出来a好像它等于0,但它不是 - 它仍然等于1E-9。
If you actually want to round the values in your array in a way that affects how they will be used in calculations, you should use np.round, as others have already pointed out.
如果您确实想以影响它们在计算中使用方式的方式舍入数组中的值,则应该使用np.round,正如其他人已经指出的那样。
回答by Roland
[ round(x,2) for x in [2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01]]
回答by rth
You can use round function. Here some example
您可以使用圆形功能。这里有一些例子
numpy.round([2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01],2)
array([ 21.53, 8.13, 3.97, 10.08])
IF you want change just display representation, I would notrecommended to alter printing format globally, as it suggested above. I would format my output in place.
如果您只想更改显示表示,我不建议像上面建议的那样全局更改打印格式。我会就地格式化我的输出。
>>a=np.array([2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01])
>>> print([ "{:0.2f}".format(x) for x in a ])
['21.53', '8.13', '3.97', '10.08']
回答by U2EF1
You're confusing actual precision and display precision. Decimal rounding cannot be represented exactly in binary. You should try:
您混淆了实际精度和显示精度。十进制舍入不能用二进制精确表示。你应该试试:
> np.set_printoptions(precision=2)
> np.array([5.333333])
array([ 5.33])

