How many distinct values in array?

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

Numpy frequency of values python

Numpy unique method

To calculate the frequency of values, use the unique function from NumPy with your array as the argument 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}")

    

Numpy frequency of values python

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.

See also  How to resolve TypeError: Cannot perform reduce with flexible type

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

Sorting the array before calculating the frequency can improve performance, especially with large datasets. This is because np.unique can take advantage of the sorted order to compute counts more efficiently.

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_values, counts = np.unique(sorted_array, return_counts=True)

print("Unique values:", unique_values)
print("Counts:", counts)
    

np.sort(my_array): Sorts the array in ascending order. The sorted array is then passed to np.unique with return_counts=True. The counts correspond to the number of times each unique value appears in the array.

See also  Exponential Regression with NumPy

Alternatively, if you’re working with pandas Series or DataFrames, you can use value_counts directly.