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 create numpy array with zeros?

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

The unique method in NumPy can be used to calculate the frequency of distinct values in an array. Note that NumPy does not have a value_counts method; this functionality is often associated with pandas. However, if the array is sorted, you can use the 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.

See also  Python in Cryptocurrency Analysis

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 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 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.

See also  How to Inverse Matrix in Numpy with Python

If you need to calculate the frequency of distinct values in a sorted array, you should use the 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.