numpy.ufunc.reduce#
method
- ufunc.reduce(array,axis=0,dtype=None,out=None,keepdims=False,initial=<novalue>,where=True)#
Reduces
array’s dimension by one, by applying ufunc along one axis.Let\(array.shape = (N_0, ..., N_i, ..., N_{M-1})\). Then\(ufunc.reduce(array, axis=i)[k_0, ..,k_{i-1}, k_{i+1}, .., k_{M-1}]\) =the result of iteratingj over\(range(N_i)\), cumulatively applyingufunc to each\(array[k_0, ..,k_{i-1}, j, k_{i+1}, .., k_{M-1}]\).For a one-dimensional array, reduce produces results equivalent to:
r=op.identity# op = ufuncforiinrange(len(A)):r=op(r,A[i])returnr
For example, add.reduce() is equivalent to sum().
- Parameters:
- arrayarray_like
The array to act on.
- axisNone 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 firstdimension of the input array.axis may be negative, inwhich case it counts from the last to the first axis.
If this is None, a reduction is performed over all the axes.If this is a tuple of ints, a reduction is performed on multipleaxes, 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. Theufuncs do not currently raise an exception in this case, but willlikely do so in the future.
- dtypedata-type code, optional
The data type used to perform the operation. Defaults to that of
outif given, and the data type ofarrayotherwise (thoughupcast to conserve precision for some cases, such asnumpy.add.reducefor integer or boolean input).- outndarray, None, …, or tuple of ndarray and None, optional
Location into which the result is stored.If not provided or None, a freshly-allocated array is returned.If passed as a keyword argument, can be Ellipses (
out=...) toensure an array is returned even if the result is 0-dimensional(which is useful especially for object dtype), or a 1-element tuple(latter for consistency withufunc.__call__).New in version 2.3:Support for
out=...was added.- keepdimsbool, optional
If 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 original
array.- initialscalar, optional
The value with which to start the reduction.If the ufunc has no identity or the dtype is object, this defaultsto None - otherwise it defaults to ufunc.identity.If
Noneis given, the first element of the reduction is used,and an error is thrown if the reduction is empty.- wherearray_like of bool, optional
A boolean array which is broadcasted to match the dimensionsof
array, and selects elements to include in the reduction. Notethat for ufuncs likeminimumthat do not have an identitydefined, one has to pass in alsoinitial.
- Returns:
- rndarray
The reduced array. Ifout was supplied,r is a reference to it.
Examples
>>>importnumpyasnp>>>np.multiply.reduce([2,3,5])30
A multi-dimensional array example:
>>>X=np.arange(8).reshape((2,2,2))>>>Xarray([[[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 0array([[ 4, 6], [ 8, 10]])>>>np.add.reduce(X,1)array([[ 2, 4], [10, 12]])>>>np.add.reduce(X,2)array([[ 1, 5], [ 9, 13]])
You can use the
initialkeyword argument to initialize the reductionwith a different value, andwhereto select specific elements to include:>>>np.add.reduce([10],initial=5)15>>>np.add.reduce(np.ones((2,2,2)),axis=(0,2),initial=10)array([14., 14.])>>>a=np.array([10.,np.nan,10])>>>np.add.reduce(a,where=~np.isnan(a))20.0
Allows reductions of empty arrays where they would normally fail, i.e.for ufuncs without an identity.
>>>np.minimum.reduce([],initial=np.inf)inf>>>np.minimum.reduce([[1.,2.],[3.,4.]],initial=10.,where=[True,False])array([ 1., 10.])>>>np.minimum.reduce([])Traceback (most recent call last):...ValueError:zero-size array to reduction operation minimum which has no identity