Calculate the sum of each row in an array using NumPy.

In this python numpy program, we will calculate the sum of each row in an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Use np.sum() and set the axis equal to 1 to calculate the sum of each row in an array.
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of each row : ",np.sum(x,axis=1))
				
			

Output :

				
					Sum of each row :  [ 8 11]
				
			

calculate the sum of all elements in an array.

calculate the sum of each column in an array

Leave a Comment