numpy.ndarray指定每个元素保留小数点后多少位---np.around

问题

我在采用round处理一个np.ndarray数组时,报出一个错误:

TypeError: type numpy.ndarray doesn't define __round__ method

解决

采用numpy.around()函数,它类似于Python原生的round()函数。

numpy.around参数说明

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

例子

>>> np.around([0.37, 1.64])
array([ 0.,  2.])
>>> np.around([0.37, 1.64], decimals=1)
array([ 0.4,  1.6])
>>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([ 0.,  2.,  2.,  4.,  4.])
>>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
array([ 1,  2,  3, 11])
>>> np.around([1,2,3,11], decimals=-1)
array([ 0,  0,  0, 10])

猜你喜欢

转载自blog.csdn.net/dta0502/article/details/83961494