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):
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)