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。(a,小数=2)只给了我[2.15300000e+01, 8.13000000e+00, 3.97000000e+00, 1.00800000e+01],我不想这样做,也没找到别的方法。
4 个解决方案
#1
45
In order to make numpy display float 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 = lambda x: "%.2f" % x
The f
here means fixed-point format (not 'scientific'), and the .2
means 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 only affects 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 a
as if it were equal to 0
, but it is not - it still equals 1E-9
.
numpy打印出a,好像它等于0,但它不是-它仍然等于1 -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。就像其他人已经指出的那样。
#2
26
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])
#3
15
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 not recommended 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']
#4
0
[ round(x,2) for x in [2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01]]
#1
45
In order to make numpy display float 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 = lambda x: "%.2f" % x
The f
here means fixed-point format (not 'scientific'), and the .2
means 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 only affects 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 a
as if it were equal to 0
, but it is not - it still equals 1E-9
.
numpy打印出a,好像它等于0,但它不是-它仍然等于1 -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。就像其他人已经指出的那样。
#2
26
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])
#3
15
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 not recommended 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']
#4
0
[ round(x,2) for x in [2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01]]