0

The accepted answer of the most frequent occurrence is

a = np.array([1,2,3,1,2,1,1,1,3,2,2,1])
counts = np.bincount(a)
print(np.argmax(counts))

But I'm wondering if anybody has an elegant way of finding the top 10 most frequent, for example. Thanks

Tomk07
  • 113
  • 2
  • 12

2 Answers2

1

Here you are:

for i in range(10):
  largest = np.argmax(counts)
  counts[largest] = 0
  print(largest)
tayler6000
  • 103
  • 1
  • 8
1

Here is a very nice answer in the original post that I had missed from Apogentus

values, counts = np.unique(a, return_counts=True)

ind = np.argmax(counts)
print(values[ind])  # prints the most frequent element

ind = np.argpartition(-counts, kth=10)[:10]
print(values[ind])  # prints the 10 most frequent elements
Tomk07
  • 113
  • 2
  • 12