Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a NumPy array by frequency?

I am attempting to sort a NumPy array by frequency of elements. So for example, if there's an array [3,4,5,1,2,4,1,1,2,4], the output would be another NumPy sorted from most common to least common elements (no duplicates). So the solution would be [4,1,2,3,5]. If two elements have the same number of occurrences, the element that appears first is placed first in the output. I have tried doing this, but I can't seem to get a functional answer. Here is my code so far:

temp1 = problems[j]
indexes = np.unique(temp1, return_index = True)[1]
temp2 = temp1[np.sort(indexes)]
temp3 = np.unique(temp1, return_counts = True)[1]
temp4 = np.argsort(temp3)[::-1] + 1

where problems[j] is a NumPy array like [3,4,5,1,2,4,1,1,2,4]. temp4 returns [4,1,2,5,3] so far but it is not correct because it can't handle when two elements have the same number of occurrences.

like image 383
Archie Shahidullah Avatar asked Aug 08 '18 00:08

Archie Shahidullah


People also ask

How to sort elements of an array by frequency in Python?

Here in this Article we will learn about Python Program for sorting elements of an Array by Frequency . We need to print the elements of an array in the descending order of their frequency and if two elements have same frequency then print the one which came first.

How to sort a NumPy array?

Here, arr is a numpy array (that is, a numpy ndarray object). Let’s look at some examples and use-cases of sorting a numpy array. 1. Sort a 1-D numpy array We can use the numpy ndarray sort () function to sort a one-dimensional numpy array.

How to reverse the Order of a sorted array in Python?

But you can use slicing to reverse the order of a sorted array. See the example below: Here, np.sort (arr) returns a sorted copy of the original array in ascending order which is then reversed using the slicing operator with a -1 step size, [::-1].

How to sort array elements according to their counts in MySQL?

Using a hashing mechanism, we can store the elements (also first index) and their counts in a hash. Finally, sort the hash elements according to their counts. This can also be solved by Using two maps, one for array element as an index and after this second map whose keys are frequency and value are array elements.


2 Answers

You can use argsort on the frequency of each element to find the sorted positions and apply the indexes to the unique element array

unique_elements, frequency = np.unique(array, return_counts=True)
sorted_indexes = np.argsort(frequency)[::-1]
sorted_by_freq = unique_elements[sorted_indexes]
like image 176
tommyp Avatar answered Oct 14 '22 01:10

tommyp


A non-NumPy solution, which does still work with NumPy arrays, is to use an OrderedCounter followed by sorted with a custom function:

from collections import OrderedDict, Counter

class OrderedCounter(Counter, OrderedDict):
    pass

L = [3,4,5,1,2,4,1,1,2,4]

c = OrderedCounter(L)
keys = list(c)

res = sorted(c, key=lambda x: (-c[x], keys.index(x)))

print(res)

[4, 1, 2, 3, 5]
like image 40
jpp Avatar answered Oct 14 '22 00:10

jpp