【numpy】【ndarray】指定每個元素保留小數點後多少位【around】【round】
dump_svmlight_file() 函式內部無法指定輸出結果保留小數點後多少位,因此需提前呼叫函式限制小數點位數,相當於 python 原生的 round() 函式,引出 numpy.around() 函式。
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
|
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])