方法
ufunc.
accumulate
累积将运算符应用于所有元素的结果。
对于一维数组,Accumulate生成的结果等于:
r = np.empty(len(A)) t = op.identity # op = the ufunc being applied to A's elements for i in range(len(A)): t = op(t, A[i]) r[i] = t return r
例如,add.accumulation()等价于np.cumsum()。
对于多维数组,仅沿一个轴应用“累计”(默认情况下为零轴;请参见下面的示例),因此如果要在多个轴上进行累计,则需要重复使用。
要执行的数组。
应用累计的轴;默认值为零。
用于表示中间结果的数据类型。如果提供了输出数组,则默认为输出数组的数据类型;如果没有提供输出数组,则默认为输入数组的数据类型。
存储结果的位置。如果未提供或没有,则返回新分配的数组。为了与 ufunc.__call__ ,如果作为关键字提供,则可以将其包装在一个1元素元组中。
ufunc.__call__
在 1.13.0 版更改: 关键字参数允许使用元组。
累积值。如果 out 供应, r 是指 out .
实例
一维数组示例:
>>> np.add.accumulate([2, 3, 5]) array([ 2, 5, 10]) >>> np.multiply.accumulate([2, 3, 5]) array([ 2, 6, 30])
二维数组示例:
>>> I = np.eye(2) >>> I array([[1., 0.], [0., 1.]])
沿轴0(行)累积,向下列:
>>> np.add.accumulate(I, 0) array([[1., 0.], [1., 1.]]) >>> np.add.accumulate(I) # no axis specified = axis zero array([[1., 0.], [1., 1.]])
沿轴1(列)累积,通过行:
>>> np.add.accumulate(I, 1) array([[1., 1.], [0., 1.]])