Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.ufunc.accumulate

ufunc.accumulate(array,axis=0,dtype=None,out=None)

Accumulate the result of applying the operator to all elements.

For a one-dimensional array, accumulate produces results equivalent to:

r=np.empty(len(A))t=op.identity# op = the ufunc being applied to A's  elementsforiinrange(len(A)):t=op(t,A[i])r[i]=treturnr

For example, add.accumulate() is equivalent to np.cumsum().

For a multi-dimensional array, accumulate is applied along only oneaxis (axis zero by default; see Examples below) so repeated use isnecessary if one wants to accumulate over multiple axes.

Parameters:
array:array_like

The array to act on.

axis:int, optional

The axis along which to apply the accumulation; default is zero.

dtype:data-type code, optional

The data-type used to represent the intermediate results. Defaultsto the data-type of the output array if such is provided, or thethe 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 orNone,a freshly-allocated array is returned. For consistency withufunc.__call__, if given as a keyword, this may be wrapped in a1-element tuple.

Changed in version 1.13.0:Tuples are allowed for keyword argument.

Returns:
r:ndarray

The accumulated values. Ifout was supplied,r is a reference toout.

Examples

1-D array examples:

>>>np.add.accumulate([2,3,5])array([ 2,  5, 10])>>>np.multiply.accumulate([2,3,5])array([ 2,  6, 30])

2-D array examples:

>>>I=np.eye(2)>>>Iarray([[ 1.,  0.],       [ 0.,  1.]])

Accumulate along axis 0 (rows), down columns:

>>>np.add.accumulate(I,0)array([[ 1.,  0.],       [ 1.,  1.]])>>>np.add.accumulate(I)# no axis specified = axis zeroarray([[ 1.,  0.],       [ 1.,  1.]])

Accumulate along axis 1 (columns), through rows:

>>>np.add.accumulate(I,1)array([[ 1.,  1.],       [ 0.,  1.]])

Previous topic

numpy.ufunc.reduce

Next topic

numpy.ufunc.reduceat

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp