赞
踩
dump_svmlight_file() 函数内部无法指定输出结果保留小数点后多少位,因此需提前调用函数限制小数点位数,相当于 python 原生的 round() 函数,引出 numpy.around() 函数。
numpy.
around
(a, decimals=0, out=None)
Evenly round to the given number of decimals.
参数 | a : array_like
decimals : int, optional
out : ndarray, optional
|
---|---|
返回值 | rounded_array : ndarray
|
See also
equivalent method
Notes
For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc. Results may also be surprising due to the inexact representation of decimal fractions in the IEEE floating point standard [R9] and errors introduced when scaling by powers of ten.
Examples
- >>> 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])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。