numpy数组如何将数组中的元素批量保留小数(或精确到指定位)?np.around()函数(与np.round()函数等价?)
生活随笔
收集整理的這篇文章主要介紹了
numpy数组如何将数组中的元素批量保留小数(或精确到指定位)?np.around()函数(与np.round()函数等价?)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
From numpy\core\fromnumeric.py
def around(a, decimals=0, out=None):"""Evenly round to the given number of decimals.平等地舍入到給定的小數(shù)位數(shù)。Parameters----------a : array_likeInput data.decimals : int, optionalNumber of decimal places to round to (default: 0). Ifdecimals is negative, it specifies the number of positions tothe left of the decimal point.要舍入的小數(shù)位數(shù)(默認(rèn)值:0)。 如果 decimals 值為負(fù)數(shù),則指定小數(shù)點左側(cè)的位數(shù)。out : ndarray, optionalAlternative output array in which to place the result. It must havethe same shape as the expected output, but the type of the outputvalues will be cast if necessary. See `doc.ufuncs` (Section"Output arguments") for details.放置結(jié)果的替代輸出數(shù)組。 它必須具有與預(yù)期輸出相同的形狀,但是如有必要,將強制轉(zhuǎn)換輸出值的類型。 有關(guān)詳細(xì)信息,請參見`doc.ufuncs`(“輸出參數(shù)”部分)。Returns-------rounded_array : ndarrayAn array of the same type as `a`, containing the rounded values.Unless `out` was specified, a new array is created. A reference tothe result is returned.The real and imaginary parts of complex numbers are roundedseparately. The result of rounding a float is a float.與“ a”類型相同的數(shù)組,其中包含四舍五入的值。 除非指定了`out`,否則將創(chuàng)建一個新數(shù)組。 返回對結(jié)果的引用。復(fù)數(shù)的實部和虛部分別取整。 將浮點數(shù)舍入的結(jié)果是一個浮點數(shù)。See Also--------ndarray.round : equivalent methodceil, fix, floor, rint, truncNotes-----For values exactly halfway between rounded decimal values, NumPyrounds 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 dueto the inexact representation of decimal fractions in the IEEEfloating point standard [1]_ and errors introduced when scalingby powers of ten.對于恰好介于四舍五入的十進(jìn)制值之間的值,NumPy會四舍五入為最接近的偶數(shù)。 因此,將1.5和2.5四舍五入為2.0,將-0.5和0.5四舍五入為0.0,等等。由于IEEE浮點標(biāo)準(zhǔn)[1] _中小數(shù)部分的不精確表示以及以10的冪進(jìn)行縮放時引入的誤差,結(jié)果也可能令人驚訝。 。References----------.. [1] "Lecture Notes on the Status of IEEE 754", William Kahan,http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF.. [2] "How Futile are Mindless Assessments ofRoundoff in Floating-Point Computation?", William Kahan,http://www.cs.berkeley.edu/~wkahan/Mindless.pdfExamples-------->>> 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 四舍五入到最接近的偶數(shù)值array([ 0., 2., 2., 4., 4.])>>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned 返回int的ndarrayarray([ 1, 2, 3, 11])>>> np.around([1,2,3,11], decimals=-1)array([ 0, 0, 0, 10])"""return _wrapfunc(a, 'round', decimals=decimals, out=out)參考文章1:【numpy】【ndarray】指定每個元素保留小數(shù)點后多少位【around】【round】
參考文章2:numpy數(shù)組中round, around, rint, ceil, floor, modf, trunc, fix
總結(jié)
以上是生活随笔為你收集整理的numpy数组如何将数组中的元素批量保留小数(或精确到指定位)?np.around()函数(与np.round()函数等价?)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【程序员の英文听写】Trump’s To
- 下一篇: Numpy中np.dot()与np.ma