NumPy: Count the frequency of unique values in numpy array
NumPy: Array Object Exercise-94 with Solution
Write a NumPy program to count the frequency of unique values in numpy array.
import numpy as np a = np.array( [10,10,20,10,20,20,20,30, 30,50,40,40] ) print("Original array:") print(a) unique_elements, counts_elements = np.unique(a, return_counts=True) print("Frequency of unique values of the said array:") print(np.asarray((unique_elements, counts_elements)))
Original array: [10 10 20 10 20 20 20 30 30 50 40 40] Frequency of unique values of the said array: [[10 20 30 40 50] [ 3 4 2 2 1]]
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
For-else construct useful when searched for something and find it:
# For example assume that I need to search through a list and process each item until a flag item is found and # then stop processing. If the flag item is missing then an exception needs to be raised. for i in mylist: if i == theflag: break process(i) else: raise ValueError("List argument missing terminal flag.")
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework