1.定义
老规矩,遇事不决,先查手册,下面是关于 numpy官方手册中关于np.cumsum()的定义与例子:


同时,官方提供了几个例子:
>>> a = np.array([[1,2,3], [4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> np.cumsum(a)
array([ 1, 3, 6, 10, 15, 21])
>>> np.cumsum(a, dtype=float)
# specifies type of output value(s)
array([ 1.,
3.,
6., 10., 15., 21.])
>>> np.cumsum(a,axis=0)
array([[1, 2, 3],
[5, 7, 9]])
>>> np.cumsum(a,axis=1)
array([[ 1, 3, 6],
[ 4, 9, 15]])
2. 实际用例分析
2.1在axis=None时
根据官方文档:The default (None) is to
compute the cumsum over the flattened array.
在axis=None时计算的是展平累积和:
a
Out[19]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
a.shape
Out[20]: (3, 3)
a为一个3x3的np array,当我们对其进行处理时,会得到如下结果:
(转载请注明原文链接:详细例子,np.cumsum()你只看这一篇就够了!https://blog.csdn.net/ftimes/article/details/119682910)

本文详细介绍了numpy的cumsum函数,包括其在不同axis上的应用。通过对示例的分析,解释了axis=None时的累积和计算以及axis=0和axis=1时的按行和按列累加。此外,还探讨了更高维度数组的累积和计算。文章适合想要深入理解numpy累加操作的读者。
8502

被折叠的 条评论
为什么被折叠?



