Let’s learn how to calculate frequency of distinct values in Numpy array. We will use Numpy unique method to calculate that.

## Numpy unique method

To calculate frequency of values use Numpy unique function. As arguments use your array and return_counts=True.

Then I created variable unique_values and using asarray function created new array with the values and corresponding frequencies.

import numpy as np my_array = np.array([1, 1, 2, 2, 2, 3, 3, 3, 4, 77]) unique, counts = np.unique(my_array, return_counts=True) unique_values = np.asarray((unique, counts)).T print(f"This is the frequency of values" f" in my array: \n {unique_values}")

Python printed out frequency of values. As you can see in my array I had 1, 2, 3, 4 and 77. In second column Python returned the number of these values.

## How to calculate the frequency of distinct values in a sorted array?

The ‘unique()’ and ‘value_counts()’ methods in NumPy can be used to calculate the frequency of distinct values in an array, regardless of whether the array is sorted or not. However, if the array is sorted, you can use the ‘np.sort()’ method to sort the array before you call the ‘unique()’ or ‘value_counts()’ methods. This will improve the performance of the methods, especially if the array is large.

For example, the following code:

import numpy as np my_array = np.array([1, 2, 2, 3, 3, 3, 4, 77]) sorted_array = np.sort(my_array) unique, counts = np.unique(sorted_array, return_counts=True) print(unique) print(counts)

This code will print out the unique values in the array, followed by the corresponding frequencies of those values. The ‘np.sort()’ method is used to sort the array before the ‘unique()’ method is called. This improves the performance of the ‘unique()’ method, especially because the array is large.

The following code:

import numpy as np my_array = np.array([1, 2, 2, 3, 3, 3, 4, 77]) value_counts = np.value_counts(my_array) print(value_counts)

This code will print out the same frequencies of the unique values in the array, but it will return them in a single array. The ‘np.sort()’ method is not used in this code, because the ‘value_counts()’ method does not sort the array before it calculates the frequencies of the unique values.

In general, if you need to calculate the frequency of distinct values in a sorted array, you should use the ‘np.sort()’ method to sort the array before you call the ‘unique()’ or ‘value_counts()’ methods. This will improve the performance of the methods, especially if the array is large.