Get the number of nonzero elements in an array using NumPy

In this python numpy program, we will get the number of nonzero elements 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. Get the number of nonzero elements in an array using np.count_nonzero().
  4. Print the output.
				
					import numpy as np
x = np.array([[4, 8, 0],[2, 0, 6]])
print("Number of non-zero elements: ",np.count_nonzero(x))
				
			

Output :

				
					Number of non-zero elements:  4
				
			

split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements.

find the median of the matrix.

Leave a Comment