Python :打印 x 沿轴 0 和 1 的累积和

Python :print the cumulative sum of x along axies 0 and 1

创建一个 x 形状 (5,6) 的数组,其中包含 30 个介于 -30 和 30 之间的随机整数

print the cumulative sum of x along axies 0

print the cumulative sum of x along axies 1

预期的结果是 9 和 -32。

我试过下面的代码

 import numpy as np
 np.random.seed(100)
 l1= np.random.randint(-30,30, size=(5,6))
 x= np.array(l1)
 print(x.sum(axis=0))
 print(x.sum(axis=1))

你能帮我看看这是怎么回事吗?

你的表达式结果是:

x.sum(axis=0)  ==  array([ -9, -58, -38,  40,  16,   9])
x.sum(axis=1)  ==  array([-68,  47,   1,  12, -32])

如您所写,预期结果是 9-32,也许您想要 计算最后一列最后一行?

的总和

为了得到这些结果,计算:

x[:, -1].sum()    (yields 9)
x[-1, :].sum()    (yiels -32)