Calculate the sum of all elements in an array using NumPy.

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

Steps to solve the program
  1. Import the numpy library as np.
  2. Calculate the sum of all elements in an array using np.sum().
  3. Print the output.
				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of all the elements : ",np.sum(x))
				
			

Output :

				
					Sum of all the elements :  19
				
			

create a 2×2 zero matrix with elements on the main diagonal equal to 7,8.

calculate the sum of each row in an array.

Leave a Comment