0

I am trying to create a function that returns the most common element from an array it is passed. My code looks like this:

def get_classification(classes):
    from collections import Counter
    count = Counter(classes)
    return count.most_common()[0]

And it is properly returning the most common element. However, it returns it in the format element, count such as:


3.0, 2
2.0, 3
1.0, 3

I don't want it to return that tuple. I just need it to return the most common element. How is that possible?

I have tried the solution:

counts = numpy.bincount(classes)
final = numpy.argmax(counts)

return final

But that did not work for me either. Any advice would be greatly appreciated.

ruohola
  • 21,987
  • 6
  • 62
  • 97
artemis
  • 6,857
  • 11
  • 46
  • 99

1 Answers1

1

You can use indexing again, to get the first element of the tuple:

def get_classification(classes):
    from collections import Counter
    count = Counter(classes)
    return count.most_common()[0][0]

print(get_classification([1, 3, 3, 1, 2, 1])) # ==> 1
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55