【发布时间】:2019-10-23 21:15:53
【问题描述】:
我想通过以下方式“舍入”(不精确的数学舍入)numpy 数组的元素:
给定一个数字在 0.00001 到 9.99999 之间的 numpy NxN 或 NxM 二维数组,如
a=np.array([[1.232, 1.872,2.732,0.123],
[0.0019, 0.025, 1.854, 0.00017],
[1.457, 0.0021, 2.34 , 9.99],
[1.527, 3.3, 0.012 , 0.005]]
)
我基本上想通过选择每个元素的第一个非零数字(不管第一个非零数字后面的数字)来“四舍五入”这个 numpy 数组 给出输出:
output =np.array([[1.0, 1.0, 2.0, 0.1],
[0.001, 0.02, 1.0, 0.0001],
[1.0, 0.002, 2 , 9.0],
[1, 3, 0.01 , 0.005]]
)
感谢您的帮助!
【问题讨论】:
-
使用 numpy.around
-
在 numpy around 中无法正常工作,因为当我设置值 numpy.around(a, decimals=0) 时,数字 0.0019 、 0.025 、 0.00017 等它们都变为 0 而不是 0.001、0.02、 0.0001
标签: python arrays numpy rounding