How to count the frequency of unique values in NumPy array?

Let’s see How to count the frequency of unique values in NumPy array. Python’s numpy library provides a numpy.unique() function to find the unique elements and it’s corresponding frequency in a numpy array.

Syntax: numpy.unique(arr, return_counts=False)

Return: Sorted unique elements of an array with their corresponding frequency counts NumPy array.

Now, Let’s see examples:

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
  
ini_array = np.array([10, 20, 5,
                      10, 8, 20,
                      8, 9])
  
# Get a tuple of unique values 
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array, 
                              return_counts = True)
# print unique values array
print("Unique Values:"
      unique)
  
# print frequency array
print("Frequency Values:",
      frequency)

chevron_right


Output:

Unique Values: [ 5  8  9 10 20]
Frequency Values: [1 2 1 2 2]

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
  
# create a 1d-array
ini_array = np.array([10, 20, 5,
                    10, 8, 20,
                    8, 9])
  
# Get a tuple of unique values 
# amnd their frequency 
# in numpy array
unique, frequency = np.unique(ini_array,
                              return_counts = True
  
# convert both into one numpy array
count = np.asarray((unique, frequency ))
  
print("The values and their frequency are:\n",
     count)

chevron_right


Output:

The values and their frequency are:
[[ 5  8  9 10 20]
[ 1  2  1  2  2]]

Example 3:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import library
import numpy as np
  
# create a 1d-array
ini_array = np.array([10, 20, 5,
                      10, 8, 20,
                      8, 9])
  
# Get a tuple of unique values 
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array, 
                              return_counts = True
  
# convert both into one numpy array 
# and then transpose it
count = np.asarray((unique,frequency )).T
  
print("The values and their frequency are in transpose form:\n",
     count)

chevron_right


Output:

The values and their frequency are in transpose form:
[[ 5  1]
[ 8  2]
[ 9  1]
[10  2]
[20  2]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.