numpy累积

时间:2023-03-08 16:57:03

numpy累积有两类函数:np.cumxxxxx和np.ufunc.accumulate()

import numpy as np

a = np.arange(1, 5)
print(np.cumprod(a))
print(np.cumsum(a))
print(np.add.accumulate(a))
print(np.multiply.accumulate(a))
b = np.tile(np.arange(1, 5), (4, 1))
print(np.cumsum(b, axis=1))
print(np.multiply.accumulate(b, axis=1))

输出为:

[ 1  2  6 24]
[ 1 3 6 10]
[ 1 3 6 10]
[ 1 2 6 24]
[[ 1 3 6 10]
[ 1 3 6 10]
[ 1 3 6 10]
[ 1 3 6 10]]
[[ 1 2 6 24]
[ 1 2 6 24]
[ 1 2 6 24]
[ 1 2 6 24]]