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

Time:12-31

create a array of x shape (5,6) having 30 random integer between -30 and 30

print the cumulative sum of x along axies 0

print the cumulative sum of x along axies 1

The out expected is 9 and -32.

I tryed with below code

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

can you please help me what is wrong with this?

CodePudding user response:

The results of your expressions are:

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

As you wrote the expected results are 9 and -32, maybe you want to compute sums of the last column and last row?

To get just these results, compute:

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