reduce()
沿着axis参数指定的轴对数组进行操作
>>> np.add.reduce([1,2,3]) # 1 + 2 + 3
6
>>> np.add.reduce([[1,2,3],[4,5,6]], axis=1) # (1+2+3),(4+5+6)
array([6, 15])
>>> np.multiply.reduce([5,2,3])
30
accumulate()
返回与输入数组的形状相同的数组,保存所有的中间计算结果
>>> np.add.accumulate([1,2,3])
array([1, 3, 6])
>>> np.add.accumulate([[1,2,3],[4,5,6]], axis=1)
array([[ 1, 3, 6],
[ 4, 9, 15]])
outer()
对x和y中每对元素应用原始运算。结果数组的形状为x.shape+y.shape
>>> np.multiply.outer([1,2,3,4,5],[2,3,4])
array([[ 2, 3, 4],
[ 4, 6, 8],
[ 6, 9, 12],
[ 8, 12, 16],
[10, 15, 20]])
reduceat()
通过indices指示起始和结束位置,计算出多组reduce()的结果。
其计算公式为
if indices[i] < indices[i+1]:
result[i] = <op>.reduce(a[indices[i]:indices[i+1]])
else:
result[i] = a[indices[i]]
举例
>>> np.add.reduceat(np.arange(9),[0,5,2,7])
[10 5 20 15]
第一步,取[0:5]进行reduce,得0+1+2+3+4=10
第二步,5>2,得5
第三步,取[2:7]进行reduce,得2+3+4+5+6=20
第四步,取[7:]进行reduce,得7+8=15