numpy.cumulative_prod#

numpy.cumulative_prod(x, /, *, axis=None, dtype=None, out=None, include_initial=False)[源代码]#

返回给定轴上元素的累积积.

此函数是与 Array API 兼容的 numpy.cumprod 的替代方案.

参数:
xarray_like

输入数组.

int, optional

计算累积乘积的轴.默认值 (None) 仅允许用于一维数组.对于具有多个维度的数组,需要 axis .

dtypedtype,可选

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

outndarray,可选

用于放置结果的可选输出数组.它必须具有与预期输出相同的形状和缓冲区长度,但如果需要,将转换结果值的类型.有关更多详细信息,请参见 输出类型确定 .

include_initialbool,可选

指示是否将初始值(ones)作为输出中的第一个值包含在内的布尔值.使用 include_initial=True ,输出的形状与输入的形状不同.默认值: False .

返回:
cumulative_prod_along_axisndarray

除非指定了 out ,否则将返回一个包含结果的新数组,在这种情况下,将返回对 out 的引用.如果 include_initial=False ,则结果的形状与 x 相同.

注释

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

示例

>>> a = np.array([1, 2, 3])
>>> np.cumulative_prod(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.cumulative_prod(a, dtype=float) # specify type of output
array([   1.,    2.,    6.,   24.,  120.,  720.])

b 的每列(即,在行上)的累积乘积:

>>> b = np.array([[1, 2, 3], [4, 5, 6]])
>>> np.cumulative_prod(b, axis=0)
array([[ 1,  2,  3],
       [ 4, 10, 18]])

b 的每行(即,在列上)的累积乘积:

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