numpy np.sum()函數(求給定軸上的數組元素的總和)(與ndarray.sum()函數等價)

from numpy\core\fromnumeric.py

def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, initial=np._NoValue):
    """
    Sum of array elements over a given axis.
    給定軸上的數組元素的總和。

    Parameters
    ----------
    a : array_like
        Elements to sum. 求和元素。
    axis : None or int or tuple of ints, optional
        Axis or axes along which a sum is performed.  The default,
        axis=None, will sum all of the elements of the input array.  If
        axis is negative it counts from the last to the first axis.
        執行求和的一個或多個軸。 
        默認值axis = None將對輸入數組的所有元素求和。 如果軸爲負,則從最後一個到第一個軸計數。

        .. versionadded:: 1.7.0

        If axis is a tuple of ints, a sum is performed on all of the axes
        specified in the tuple instead of a single axis or all the axes as
        before.
        如果axis是int的元組,則對元組中指定的所有軸進行求和,
        而不是像以前那樣單個軸或所有軸。
    dtype : dtype, optional
        The type of the returned array and of the accumulator in which the
        elements are summed.  The dtype of `a` is used by default unless `a`
        has an integer dtype of less precision than the default platform
        integer.  In that case, if `a` is signed then the platform integer
        is used while if `a` is unsigned then an unsigned integer of the
        same precision as the platform integer is used.
        返回的數組和累加器的類型,元素在其中累加。 
        除非默認情況下使用a的dtype,除非a的整數dtype的精度比默認平臺整數的精度低。 
        在這種情況下,如果對a進行了符號簽名,則使用平臺整數;
        如果對a進行了簽名,則使用與平臺整數精度相同的無符號整數。
    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.
        放置結果的替代輸出數組。 
        它必須具有與預期輸出相同的形狀,但是如有必要,將強制轉換輸出值的類型。
    keepdims : bool, optional
        If this is set to True, the axes which are reduced are left
        in the result as dimensions with size one. With this option,
        the result will broadcast correctly against the input array.

        If the default value is passed, then `keepdims` will not be
        passed through to the `sum` method of sub-classes of
        `ndarray`, however any non-default value will be.  If the
        sub-class' method does not implement `keepdims` any
        exceptions will be raised.

		如果將其設置爲True,則縮小的軸將保留爲尺寸1的尺寸。 
		使用此選項,結果將針對輸入數組正確廣播。

         如果傳遞了默認值,那麼keepdims不會傳遞給ndarray子類的sum方法,但是任何非默認值都會傳遞。 
         如果子類方法未實現keepdims,則將引發任何異常。
    initial : scalar, optional
        Starting value for the sum. See `~numpy.ufunc.reduce` for details.
        總和的起始值。 有關詳細信息,請參見〜numpy.ufunc.reduce。

        .. versionadded:: 1.15.0

    Returns
    -------
    sum_along_axis : ndarray
        An array with the same shape as `a`, with the specified
        axis removed.   If `a` is a 0-d array, or if `axis` is None, a scalar
        is returned.  If an output array is specified, a reference to
        `out` is returned.
        與'a'形狀相同的n個數組,但刪除了指定的軸。 
        如果`a`是一個0維數組,或者'axis'是None,則返回標量。 
        如果指定了輸出數組,則返回對“ out”的引用。

    See Also
    --------
    ndarray.sum : Equivalent method. 等效方法。

    cumsum : Cumulative sum of array elements. 數組元素的累積和。

    trapz : Integration of array values using the composite trapezoidal rule. 	
    		使用複合梯形規則對數組值進行積分。

    mean, average

    Notes
    -----
    Arithmetic is modular when using integer types, and no error is
    raised on overflow.
    使用整數類型時,算術是模塊化的,並且在溢出時不會引發錯誤。

    The sum of an empty array is the neutral element 0:
    空數組的總和是中性元素0:

    >>> np.sum([])
    0.0

    Examples
    --------
    >>> np.sum([0.5, 1.5])
    2.0
    >>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
    1
    >>> np.sum([[0, 1], [0, 5]])
    6
    >>> np.sum([[0, 1], [0, 5]], axis=0)
    array([0, 6])
    >>> np.sum([[0, 1], [0, 5]], axis=1)
    array([1, 5])

    If the accumulator is too small, overflow occurs:
    如果累加器太小,則會發生溢出:

    >>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
    -128

    You can also start the sum with a value other than zero:
    您也可以用非零值開始求和:

    >>> np.sum([10], initial=5)
    15
    """
    if isinstance(a, _gentype):
        # 2018-02-25, 1.15.0
        warnings.warn(
            "Calling np.sum(generator) is deprecated, and in the future will give a different result. "
            "Use np.sum(np.from_iter(generator)) or the python sum builtin instead.",
            DeprecationWarning, stacklevel=2)

        res = _sum_(a)
        if out is not None:
            out[...] = res
            return out
        return res

    return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
                          initial=initial)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章