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

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

numpy.unique() Syntax

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

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

Get Unique Items and Counts in Numpy Array

There are various ways to get unique items and counts in the Numpy array here we explain some generally used methods for getting unique items and counts in the Numpy array those are following.

• Using the np.unique() Function
• Using NumPy Unique Frequency
• Using NumPy in Transpose Form
• Using `numpy.bincount()`
• Using the collections.Counter() Function

Using the np.unique() Function

In this example code uses NumPy to create an array (`ini_array`) and then utilizes `np.unique()` to obtain unique values and their frequencies. The unique values are printed with “Unique Values:”, and their corresponding frequencies are printed with “Frequency Values:”.

Python3

 `# 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)`

Output:

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

UsingNumPy Unique Frequency

In this example code, utilizing NumPy, generates a 1D array (`ini_array`) and then employs `np.unique()` to obtain unique values and their frequencies. The results are combined into a single NumPy array (`count`) and printed, displaying the values and their frequencies.

Python3

 `# 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``count ``=` `np.asarray((unique, frequency ))` `print``(``"The values and their frequency are:\n"``,``     ``count)`

Output:

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

Using NumPy in Transporse Form

In this example code uses NumPy to create a 1D array (`ini_array`) and employs `np.unique()` to obtain unique values and their frequencies. The results are combined into a NumPy array (`count`), which is then transposed for a display of values and frequencies in a transposed form.

Python3

 `# 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)`

Output:

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

Using `numpy.bincount()` Function

In this example, a 1D NumPy array `arr` is created with integer values. Using `numpy.unique()`, unique values and their counts are obtained. The results are displayed, showcasing the unique values and their corresponding counts using the `numpy.bincount()` method, ensuring proper alignment with the unique values.

Python3

 `import` `numpy as np` `# Create a 1D array``arr ``=` `np.array([``10``, ``20``, ``5``, ``10``, ``8``, ``20``, ``8``, ``9``])` `# Get unique values and their counts using bincount``unique_values ``=` `np.unique(arr)``counts ``=` `np.bincount(arr)` `# Display the results``print``(``"\nMethod 2:"``)``print``(``"Unique Values:"``, unique_values)``print``(``"Counts:"``, counts[unique_values])`

Output:

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

Using the collections.Counter() Function

In this example The code employs `Counter` from the `collections` module to count occurrences of elements in the list `my_list`. It then prints the unique values and their respective counts, offering a concise summary of the element frequencies in the list.

Python3

 `from` `collections ``import` `Counter` `# Create a list``my_list ``=` `[``10``, ``20``, ``5``, ``10``, ``8``, ``20``, ``8``, ``9``]` `# Use Counter to count occurrences``counts ``=` `Counter(my_list)` `# Display the results``print``(``"Unique Values:"``, ``list``(counts.keys()))``print``(``"Counts:"``, ``list``(counts.values()))`

Output:

`Unique Values: [10, 20, 5, 8, 9]Counts: [2, 2, 1, 2, 1]`

Previous
Next