numpy數組如何將數組中的元素批量保留小數(或精確到指定位)?np.around()函數(與np.round()函數等價?)

From numpy\core\fromnumeric.py

def around(a, decimals=0, out=None):
    """
    Evenly round to the given number of decimals.
    平等地舍入到給定的小數位數。

    Parameters
    ----------
    a : array_like
        Input data.
    decimals : int, optional
        Number of decimal places to round to (default: 0).  If
        decimals is negative, it specifies the number of positions to
        the left of the decimal point.
        要舍入的小數位數(默認值:0)。 如果 decimals 值爲負數,則指定小數點左側的位數。
    out : ndarray, optional
        Alternative output array in which to place the result. It must have
        the same shape as the expected output, but the type of the output
        values will be cast if necessary. See `doc.ufuncs` (Section
        "Output arguments") for details.
        放置結果的替代輸出數組。 它必須具有與預期輸出相同的形狀,但是如有必要,將強制轉換輸出值的類型。 有關詳細信息,請參見`doc.ufuncs`(“輸出參數”部分)。

    Returns
    -------
    rounded_array : ndarray
        An array of the same type as `a`, containing the rounded values.
        Unless `out` was specified, a new array is created.  A reference to
        the result is returned.

        The real and imaginary parts of complex numbers are rounded
        separately.  The result of rounding a float is a float.

		與“ a”類型相同的數組,其中包含四捨五入的值。 
		除非指定了`out`,否則將創建一個新數組。 返回對結果的引用。
		複數的實部和虛部分別取整。 將浮點數舍入的結果是一個浮點數。

    See Also
    --------
    ndarray.round : equivalent method

    ceil, fix, floor, rint, trunc


    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 [1]_ and errors introduced when scaling
    by powers of ten.

	對於恰好介於四捨五入的十進制值之間的值,NumPy會四捨五入爲最接近的偶數。 
	因此,將1.5和2.5四捨五入爲2.0,將-0.5和0.5四捨五入爲0.0,等等。
	由於IEEE浮點標準[1] _中小數部分的不精確表示以及以10的冪進行縮放時引入的誤差,結果也可能令人驚訝。 。

    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 of
           Roundoff in Floating-Point Computation?", William Kahan,
           http://www.cs.berkeley.edu/~wkahan/Mindless.pdf

    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 返回int的ndarray
    array([ 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】指定每個元素保留小數點後多少位【around】【round】

參考文章2:numpy數組中round, around, rint, ceil, floor, modf, trunc, fix

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章