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
indices : array_like
axis : int, optional
dtype : data-type-code, optional
out : ndarray, optional
|
---|---|
Returns: | r : array
|
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])