Summarize all columns of an array using numpy. Any better way to do it?

2

I did so:

import numpy as np

#soma de todas as colunas de mat!

mat = np.arange(1,26).reshape(5,5)
print(mat)

lista =[]
for i in np.arange(0,len(mat)):
    lista.append(np.sum(mat[0:,i:i+1]))

print(np.array(lista))

The output is correct:

[55 60 65 70 75]

Is there any better way to do it using some numpy function?

    
asked by anonymous 05.05.2018 / 22:42

1 answer

2

You can compute the sum of the columns directly with the sum function by entering the parameter: axis 0 (' axis = 0 '):

In [20]: mat.sum(axis=0)
Out[20]: array([55, 60, 65, 70, 75])

Documentation: sum

    
05.05.2018 / 23:24