How many distinct values in array?

Learn how to calculate frequency of distinct values in NumPy arrays using np.unique() with return_counts parameter.

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 rotate a matrix with Numpy

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() already sorts internally; pre-sorting rarely improves performance; may slightly help with already-sorted data but generally unnecessary.

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  Managing BufferError: Understanding Buffer Interface in NumPy

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