numpy.ufunc.reduceat

ufunc.reduceat(self, array, indices, axis=None, dtype=None, out=None)

Reduceat performs a reduce with specified slices over an axis.

Computes op.reduce(array[indices[i]:indices[i+1]]) for i=0..end with an implicit indices[i+1] = len(array) assumed when i = end - 1.

If indices[i] >= indices[i + 1] then the result is array[indices[i]] for that value.

The function op.accumulate(array) is the same as op.reduceat(array, indices)[::2] where indices is range(len(array)-1) with a zero placed in every other sample: indices = zeros(len(array)*2 - 1) indices[1::2] = range(1, len(array))

The output shape is based on the size of indices.

Parameters:

array : array_like

The array to act on.

indices : array_like

Paired indices specifying slices to reduce.

axis : int, optional

The axis along which to apply the reduceat.

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, optional

A location into which the result is stored. If not provided a freshly-allocated array is returned.

Returns:

r : array

The reduced values. If out was supplied, r is equal to out.

Examples

To take the running sum of four successive values:

>>> np.add.reduceat(np.arange(8),[0,4, 1,5, 2,6, 3,7])[::2]
array([ 6, 10, 14, 18])

Previous topic

numpy.ufunc.accumulate

Next topic

numpy.ufunc.outer

This Page

Quick search