Skip to content
Advertisement

numpy array –> sort descending unique values base count of values

I have a numpy array:

JavaScript

I want the same unique array back, but sorted descending in the order of occurrences of the elements. In this example, 6 occurs 3 times, 1 occurs 2 times and all the other elements only occur 1 time, so the result should be:

JavaScript

Does anyone know how this can be done?

Thanks in advance!

Advertisement

Answer

With pure numpy, you can use numpy.unique with return_counts=True, then numpy.argsort:

JavaScript

output: array([ 6, 1, 3, 5, 7, 9, 10])

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement