numpy.cumprod#

numpy.cumprod(a, axis=None, dtype=None, out=None)[源代码]#

返回沿给定轴的元素累积乘积.

参数:
aarray_like

输入数组.

axisint, 可选

计算累积乘积的轴.默认情况下,输入是扁平化的.

dtypedtype, 可选

返回数组的类型,以及元素相乘的累加器的类型.如果未指定 dtype ,则默认为 a 的 dtype ,除非 a 具有精度低于默认平台整数的整数 dtype .在这种情况下,使用默认平台整数.

outndarray, 可选

要在其中放置结果的替代输出数组.它必须具有与预期输出相同的形状和缓冲区长度,但如果需要,结果值的类型将被强制转换.

返回:
cumprodndarray

除非指定了 out ,否则将返回一个包含结果的新数组,在这种情况下,将返回对 out 的引用.

参见

cumulative_prod

cumprod 兼容的数组 API 替代方案.

输出类型确定

备注

使用整数类型时,算术是模运算,溢出时不会引发错误.

示例

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> np.cumprod(a) # intermediate results 1, 1*2
...               # total product 1*2*3 = 6
array([1, 2, 6])
>>> a = np.array([[1, 2, 3], [4, 5, 6]])
>>> np.cumprod(a, dtype=float) # specify type of output
array([   1.,    2.,    6.,   24.,  120.,  720.])

每个列(即,在行上)的累积积 a:

>>> np.cumprod(a, axis=0)
array([[ 1,  2,  3],
       [ 4, 10, 18]])

每个行的累积乘积(即在列上)`a`:

>>> np.cumprod(a,axis=1)
array([[  1,   2,   6],
       [  4,  20, 120]])