def reduce(self, a, axis=0, dtype=None, out=None, keepdims=False): # real signature unknown; restored from __doc__
"""
reduce(a, axis=0, dtype=None, out=None, keepdims=False)
Reduces `a`'s dimension by one, by applying ufunc along one axis.
通过沿一个轴应用ufunc将`a`的尺寸减小一倍。
Let :math:`a.shape = (N_0, ..., N_i, ..., N_{M-1})`. Then
:math:`ufunc.reduce(a, axis=i)[k_0, ..,k_{i-1}, k_{i+1}, .., k_{M-1}]` =
the result of iterating `j` over :math:`range(N_i)`, cumulatively applying
ufunc to each :math:`a[k_0, ..,k_{i-1}, j, k_{i+1}, .., k_{M-1}]`.
For a one-dimensional array, reduce produces results equivalent to:
::
令a.shape =(N_0,...,N_i,...,N_ {M-1})`。
然后:ufunc.reduce(a,axis = i)[k_0,..,k_ {i-1},k_ {i + 1},..,k_ {M-1}]`=的结果在:math:`range(N_i)`上迭代`j`,
将ufunc累积应用于每个:math:`a [k_0,..,k_ {i-1},j,k_ {i + 1},.., k_ {M-1}]`。
对于一维数组,reduce产生的结果等于:::
r = op.identity # op = ufunc
for i in range(len(A)):
r = op(r, A[i])
return r
For example, add.reduce() is equivalent to sum().
例如,add.reduce()等同于sum()。
Parameters
----------
a : array_like
The array to act on.
要作用的数组。
axis : None or int or tuple of ints, optional
Axis or axes along which a reduction is performed.
The default (`axis` = 0) is perform a reduction over the first
dimension of the input array. `axis` may be negative, in
which case it counts from the last to the first axis.
进行缩小的一个或多个轴。
默认值(“ axis” = 0)是对输入数组的第一维进行缩减。
“轴”可能为负,在这种情况下,它从最后一个轴开始计数。
.. versionadded:: 1.7.0
If this is `None`, a reduction is performed over all the axes.
If this is a tuple of ints, a reduction is performed on multiple
axes, instead of a single axis or all the axes as before.
如果为“无”,则对所有轴进行归约。
如果这是一个整数元组,则在多个轴上执行归约,而不是像以前那样在单个轴或所有轴上执行归约。
For operations which are either not commutative or not associative,
doing a reduction over multiple axes is not well-defined. The
ufuncs do not currently raise an exception in this case, but will
likely do so in the future.
对于非交换或非关联运算,在多个轴上进行归约的定义不明确。
在这种情况下,ufunc当前不会引发异常,但将来可能会引发异常。
dtype : data-type code, optional
The type used to represent the intermediate results. Defaults
to the data-type of the output array if this is provided, or
the data-type of the input array if no output array is provided.
用于表示中间结果的类型。
如果提供此参数,则默认为输出数组的数据类型;
如果不提供输出数组,则默认为输入数组的数据类型。
out : ndarray, None, or tuple of ndarray and None, optional
A location into which the result is stored. If not provided or `None`,
a freshly-allocated array is returned. For consistency with
:ref:`ufunc.__call__`, if given as a keyword, this may be wrapped in a
1-element tuple.
结果存储的位置。 如果未提供或没有,则返回一个新分配的数组。
为了与ufunc .__ call__保持一致,如果作为关键字给出,则可以将其包装在1元素元组中。
.. versionchanged:: 1.13.0
Tuples are allowed for keyword argument.
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 original `arr`.
如果将其设置为True,则缩小的轴将保留为尺寸1的尺寸。
使用此选项,结果将针对原始`arr`正确广播。
.. versionadded:: 1.7.0
Returns
-------
r : ndarray
The reduced array. If `out` was supplied, `r` is a reference to it.
精简数组。 如果提供了`out`,则`r`是对它的引用。
Examples
--------
>>> np.multiply.reduce([2,3,5])
30
A multi-dimensional array example:
>>> X = np.arange(8).reshape((2,2,2))
>>> X
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.add.reduce(X, 0) # 次外层相加
array([[ 4, 6],
[ 8, 10]])
>>> np.add.reduce(X) # confirm: default axis value is 0 如果不指定轴,则默认为0(次外层)
array([[ 4, 6],
[ 8, 10]])
>>> np.add.reduce(X, 1)
array([[ 2, 4],
[10, 12]])
>>> np.add.reduce(X, 2)
array([[ 1, 5],
[ 9, 13]])
"""
pass
参考文章:numpy高级应用—ufunc