numpy.cumprod#

numpy.cumprod(a, axis=None, dtype=None, out=None)[source]#

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

参数:
aarray_like

输入数组。

axis整数,可选

计算累积乘积的轴。默认情况下,输入会被展平。

dtype数据类型,可选

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

outndarray,可选

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

返回:
cumprodndarray

除非指定了 out,否则将返回一个包含结果的新数组;如果指定了 out,则返回对 out 的引用。

另请参阅

cumulative_prod

与数组 API 兼容的 cumprod 替代方案。

输出类型确定

注意

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

示例

>>> 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]])