ufunc.reduce(a, axis=0, dtype=None, out=None, keepdims=False)
Reduces a
’s dimension by one, by applying ufunc along one axis.
Let . Then = the result of iterating j
over , cumulatively applying ufunc to each . For a one-dimensional array, reduce produces results equivalent to:
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().
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 ( New in version 1.7.0. If this is 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. 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 Changed in version 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 New in version 1.7.0. |
---|---|
Returns: |
r : ndarray The reduced array. If |
>>> 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 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]])
© 2008–2017 NumPy Developers
Licensed under the NumPy License.
https://docs.scipy.org/doc/numpy-1.14.2/reference/generated/numpy.ufunc.reduce.html