方式一:

import numpy as np

data =np.random.rand(2,3)
print(data)

np.set_printoptions(precision=4)
print(data)

参考:https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html

方式二:

import numpy as np 

#不保留小数
np.around([0.37, 1.64]) #array([ 0.,  2.])
np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
#array([ 0.,  2.,  2.,  4.,  4.])

#保留一位小数
np.around([0.37, 1.64], decimals=1) # array([ 0.4,  1.6])
np.around([1,2,3,11], decimals=1)   #array([ 1,  2,  3, 11])

np.around([1,2,3,11], decimals=-1)
array([ 0,  0,  0, 10])

参考:https://numpy.org/doc/stable/reference/generated/numpy.around.html

numpy.around(a, decimals=0, out=None)

#a为输入列表或矩阵;
#decimals为n对输入近似后保留小数点后n位,默认为0,若值为-n,则对小数点左边第n位近似;
#out为可选参数,一般不用,用于保存近似返回结果。
Last modification:October 9th, 2021 at 01:22 pm