How do I add the components of each row of a matrix with numpy?

0

Good, I have to do a matrix problem with the numpy but I can not find a way to fix it. I would appreciate your help. The problem is this:

Program a function sum_fila (mat) that given a numpy array of rank 2 mat (a matrix) return a numpy array with the sum of each of its rows.

Entry:

suma_fila(np.array([[1, 0, -1], [3, 4, 1]]))

Solution:

array([0, 8])
    
asked by Marc TC 23.12.2016 в 01:20
source

1 answer

0

You must use numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False) with axis = 1

That is:

import numpy as np

array = np.array([[1, 0, -1], [3, 4, 1]])
salida = np.sum(array, axis=1)
print(salida)

Exit:

[0, 8]
    
answered by 23.12.2016 / 01:35
source