numpy np.sum()函数(求给定轴上的数组元素的总和)(与ndarray.sum()函数等价)
生活随笔
收集整理的這篇文章主要介紹了
numpy np.sum()函数(求给定轴上的数组元素的总和)(与ndarray.sum()函数等价)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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.給定軸上的數(shù)組元素的總和。Parameters----------a : array_likeElements to sum. 求和元素。axis : None or int or tuple of ints, optionalAxis or axes along which a sum is performed. The default,axis=None, will sum all of the elements of the input array. Ifaxis is negative it counts from the last to the first axis.執(zhí)行求和的一個(gè)或多個(gè)軸。 默認(rèn)值axis = None將對(duì)輸入數(shù)組的所有元素求和。 如果軸為負(fù),則從最后一個(gè)到第一個(gè)軸計(jì)數(shù)。.. versionadded:: 1.7.0If axis is a tuple of ints, a sum is performed on all of the axesspecified in the tuple instead of a single axis or all the axes asbefore.如果axis是int的元組,則對(duì)元組中指定的所有軸進(jìn)行求和,而不是像以前那樣單個(gè)軸或所有軸。dtype : dtype, optionalThe type of the returned array and of the accumulator in which theelements are summed. The dtype of `a` is used by default unless `a`has an integer dtype of less precision than the default platforminteger. In that case, if `a` is signed then the platform integeris used while if `a` is unsigned then an unsigned integer of thesame precision as the platform integer is used.返回的數(shù)組和累加器的類型,元素在其中累加。 除非默認(rèn)情況下使用a的dtype,除非a的整數(shù)dtype的精度比默認(rèn)平臺(tái)整數(shù)的精度低。 在這種情況下,如果對(duì)a進(jìn)行了符號(hào)簽名,則使用平臺(tái)整數(shù);如果對(duì)a進(jìn)行了簽名,則使用與平臺(tái)整數(shù)精度相同的無(wú)符號(hào)整數(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.放置結(jié)果的替代輸出數(shù)組。 它必須具有與預(yù)期輸出相同的形狀,但是如有必要,將強(qiáng)制轉(zhuǎn)換輸出值的類型。keepdims : bool, optionalIf this is set to True, the axes which are reduced are leftin 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 bepassed through to the `sum` method of sub-classes of`ndarray`, however any non-default value will be. If thesub-class' method does not implement `keepdims` anyexceptions will be raised.如果將其設(shè)置為True,則縮小的軸將保留為尺寸1的尺寸。 使用此選項(xiàng),結(jié)果將針對(duì)輸入數(shù)組正確廣播。如果傳遞了默認(rèn)值,那么keepdims不會(huì)傳遞給ndarray子類的sum方法,但是任何非默認(rèn)值都會(huì)傳遞。 如果子類方法未實(shí)現(xiàn)keepdims,則將引發(fā)任何異常。initial : scalar, optionalStarting value for the sum. See `~numpy.ufunc.reduce` for details.總和的起始值。 有關(guān)詳細(xì)信息,請(qǐng)參見?numpy.ufunc.reduce。.. versionadded:: 1.15.0Returns-------sum_along_axis : ndarrayAn array with the same shape as `a`, with the specifiedaxis removed. If `a` is a 0-d array, or if `axis` is None, a scalaris returned. If an output array is specified, a reference to`out` is returned.與'a'形狀相同的n個(gè)數(shù)組,但刪除了指定的軸。 如果`a`是一個(gè)0維數(shù)組,或者'axis'是None,則返回標(biāo)量。 如果指定了輸出數(shù)組,則返回對(duì)“ out”的引用。See Also--------ndarray.sum : Equivalent method. 等效方法。cumsum : Cumulative sum of array elements. 數(shù)組元素的累積和。trapz : Integration of array values using the composite trapezoidal rule. 使用復(fù)合梯形規(guī)則對(duì)數(shù)組值進(jìn)行積分。mean, averageNotes-----Arithmetic is modular when using integer types, and no error israised on overflow.使用整數(shù)類型時(shí),算術(shù)是模塊化的,并且在溢出時(shí)不會(huì)引發(fā)錯(cuò)誤。The sum of an empty array is the neutral element 0:空數(shù)組的總和是中性元素0:>>> np.sum([])0.0Examples-------->>> 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:如果累加器太小,則會(huì)發(fā)生溢出:>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)-128You 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.0warnings.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[...] = resreturn outreturn resreturn _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,initial=initial)總結(jié)
以上是生活随笔為你收集整理的numpy np.sum()函数(求给定轴上的数组元素的总和)(与ndarray.sum()函数等价)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数据挖掘(Data mining、资料探
- 下一篇: 【深度学习的数学】接“2×3×1层带si