如何舍入一个numpy数组? [英] How to round a numpy array?
本文介绍了如何舍入一个numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 numpy 数组,如下所示:
I have a numpy array, something like below:
data = np.array([ 1.60130719e-01, 9.93827160e-01, 3.63108206e-04])
我想将每个元素四舍五入到两位小数.
and I want to round each element to two decimal places.
我该怎么做?
推荐答案
Numpy 提供了两种相同的方法来执行此操作.要么使用
Numpy provides two identical methods to do this. Either use
np.round(data, 2)
或
np.around(data, 2)
因为它们是等价的.
请参阅文档 了解更多信息.
See the documentation for more information.
示例:
>>> 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])
这篇关于如何舍入一个numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文