gpt4 book ai didi

python - 如何舍入一个numpy数组?

转载 作者:IT老高 更新时间:2023-10-28 20:33:52 27 4
gpt4 key购买 nike

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

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

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

我该怎么做?

最佳答案

Numpy 提供了两种相同的方法来执行此操作。要么使用

np.round(data, 2)

np.around(data, 2)

因为它们是等价的。

the documentation了解更多信息。


例子:

>>> 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])

关于python - 如何舍入一个numpy数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46994426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com