python get a vector of row sums from an array

Solutions on MaxInterview for python get a vector of row sums from an array by the best coders in the world

showing results for - "python get a vector of row sums from an array"
Erika
14 Mar 2020
1# Basic syntax:
2np.sum(your_array, axis=1).tolist()
3# Where axis=1 sums across rows and axis=0 sums across columns
4
5# Example usage:
6import numpy as np
7your_array = np.array([range(0,4),range(3,7),range(1,5),range(2,6)])
8print(your_array)
9--> [[0 1 2 3]
10 	 [3 4 5 6]
11     [1 2 3 4]
12     [2 3 4 5]]
13
14np.sum(your_array, axis=0).tolist() # Return column sums
15--> [6, 10, 14, 18]
16np.sum(your_array, axis=1).tolist() # Return row sums
17--> [6, 18, 10, 14]