Python 如何舍入一个numpy数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46994426/
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-19 17:58:11  来源:igfitidea点击:

How to round a numpy array?

pythonarraysnumpyrounding

提问by ajayramesh

I have a numpy array, something like below:

我有一个 numpy 数组,如下所示:

data = np.array([  1.60130719e-01,   9.93827160e-01,   3.63108206e-04])

and I want to round each element to two decimal places.

我想将每个元素四舍五入到两位小数。

How can I do so?

我怎么能这样做?

回答by Joe Iddon

Numpy provides two identical methods to do this. Either use

Numpy 提供了两种相同的方法来做到这一点。要么使用

np.round(data, 2)

or

或者

np.around(data, 2)

as they are equivalent.

因为它们是等价的。

See the documentationfor more information.

有关更多信息,请参阅文档



Examples:

例子:

>>> import numpy as np
>>> a = np.array([0.015, 0.235, 0.112])
>>> np.round(a, 2)
array([0.02, 0.24, 0.11])
>>> np.around(a, 2)
array([0.02, 0.24, 0.11])
>>> np.round(a, 1)
array([0. , 0.2, 0.1])

回答by jmd_dk

If you want the output to be

如果您希望输出为

array([1.6e-01, 9.9e-01, 3.6e-04])

the problem is not really a missing feature of NumPy, but rather that this sort of rounding is not a standard thing to do. You can make your own rounding function which achieves this like so:

问题并不是 NumPy 真正缺失的功能,而是这种舍入不是标准的做法。您可以制作自己的舍入函数,如下所示:

def my_round(value, N):
    exponent = np.ceil(np.log10(value))
    return 10**exponent*np.round(value*10**(-exponent), N)

For a general solution handling 0and negative values as well, you can do something like this:

对于一般解决方案处理0和负值,您可以执行以下操作:

def my_round(value, N):
    value = np.asarray(value).copy()
    zero_mask = (value == 0)
    value[zero_mask] = 1.0
    sign_mask = (value < 0)
    value[sign_mask] *= -1
    exponent = np.ceil(np.log10(value))
    result = 10**exponent*np.round(value*10**(-exponent), N)
    result[sign_mask] *= -1
    result[zero_mask] = 0.0
    return result

回答by Matthew Salvatore Viglione

It is worth noting that the accepted answer will round small floats down to zero.

值得注意的是,接受的答案会将小浮点数舍入为零。

>>> import numpy as np 
>>> arr = np.asarray([2.92290007e+00, -1.57376965e-03, 4.82011728e-08, 1.92896977e-12])
>>> print(arr)
[ 2.92290007e+00 -1.57376965e-03  4.82011728e-08  1.92896977e-12]
>>> np.round(arr, 2)
array([ 2.92, -0.  ,  0.  ,  0.  ]) 

You can use set_printoptionsand a custom formatter to fix this and get a more numpy-esque printout with fewer decimal places:

您可以使用set_printoptions自定义格式化程序来解决此问题,并获得小数位数更少的更numpy-esque 打印输出:

>>> np.set_printoptions(formatter={'float': "{0:0.2e}".format})
>>> print(arr)
[2.92e+00 -1.57e-03 4.82e-08 1.93e-12]  

This way, you get the full versatility of formatand maintain the full precision of numpy's datatypes.

通过这种方式,您可以获得formatnumpy 数据类型的完整多功能性并保持其完整精度。

Also note that this only affects printing, not the actual precision of the stored values used for computation.

另请注意,这仅影响打印,而不影响用于计算的存储值的实际精度。