How to calculate mode in python?

Mode is the value with the highest frequencies in a data set. Consider a list of items, list_1 = [1, 3, 4, 3]. In list_1, the mode is 3. This article discusses different methods to find mode in a data set.

Calculating mode using mode() function

Python supports a built-in module known as statistics, to calculate statistics of numeric data. Statistics module is embedded with various functions such as mean(), median(), mode(), etc.

The mode() function takes a sequence(list, tuple, set) of numbers or strings as an argument and returns the item with the highest number of occurrences. The syntax for mode() function is given below.

#syntax:
statistics.mode(sequence)

Consider a data set list_1 = [1, 1, 1, 5, 6, 3]. The statistics.mode() function takes list_1 as argument and return 1 as the value.

import statistics
list_1 = [1, 1, 1, 5, 6, 3]
print("list_1: ", list_1)
val = statistics.mode(list_1)
print("The mode is: ", val)

Output

list_1:  [1, 1, 1, 5, 6, 3]
The mode is:  1

What if we have two items with the same frequencies? In mathematics, when we have multiple items with the highest frequencies, the smallest item with the highest frequencies is considered as mode.

In the earlier versions of python, it raises the StatisticsError exception.

import statistics
list_1 = [1, 1, 3, 3, 5]
print("list_1: ", list_1)
val = statistics.mode(list_1)
print("The mode is: ", val)

Output

'no unique mode; found %d equally common values' % len(table)
statistics.StatisticsError: no unique mode; found 2 equally common values

In newer versions of python 3.8+, the program gives the output as the smallest value with the highest frequencies. Consider a list list_1 = [1, 1, 3, 3, 5]. The item in 1 and 3 have the same frequencies. The code returns the output as 1.

list_1:  [1, 1, 3, 3, 5]
The mode is:  1

Calculating mode using collections.Counter() function

Counter() function is imported from collections module. The Counter() function holds a sequence(list, tuple, dictionary, string) as a parameter. Counter() returns a dictionary where data items of sequence are stored as keys and the frequency of each element is stored as a value.

Consider a data set list_1 = [1, 1, 5, 6, 3, 3]. The Counter() function returns the data item and frequency of the data item in list_1 as a key-value pair. The variable dict_1 = {1: 2, 5: 1, 6: 1, 3: 2} stores the key-value pair returned by Counter() function.

max(dict_1.values()) returns the maximum value in dict_1 that is 2. Now, 2 is the highest frequency for both items 1 and 3. To get the smallest value with the highest frequency we sort the dictionary and search for the key with value 2. The code gives mode as 1. 1 is the smallest value with the highest frequency.

import collections
list_1 = [1, 1, 5, 6, 3, 3]
print("list_1: ", list_1)
dict_1 =dict(collections.Counter(list_1))
print("dict_1: ", dict_1)
highest_freq = max(dict_1.values())
print("highest_freq: ", highest_freq)
for key in sorted(dict_1):
    if dict_1[key] == highest_freq:
        print("The mode is: ", key)
        break

Output

list_1:  [1, 1, 5, 6, 3, 3]
dict_1:  {1: 2, 5: 1, 6: 1, 3: 2}
highest_freq:  2
The mode is:  1
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions