In this python numpy program, we will count the frequency of unique values from the NumPy array.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Get the unique values and their frequency from the array using np.unique(array,return_counts=True).
- Print the output.
import numpy as np
x = np.array([8, 6, 7, 0, 7, 8, 6, 6])
unique, count = np.unique(x, return_counts=True)
print("Frequency of unique values in the given array:")
print(np.asarray((unique, count)))
Output :
Frequency of unique values in the given array:
[[0 6 7 8]
[1 3 2 2]]