How to find the mode of a list when there are multiple modes – Python


I am trying to find out the mode of a list that works, but when there are multiple modes, an error is returned. How would I fix this error in my code?

I want to make it so that it calculates it like how this would be calculated normally [3, 3, 4, 4] => (3+4)/2 = 3.5 if that is possible.

import statistics

numbers = ['3', '3', '4', '4']
mode = statistics.mode(numbers)
print(f'Mode: {mode}')

this is the error I get: statistics.StatisticsError: no unique mode; found 2 equally common values

Asked By: muckywater



You likely have an old python version. In recent versions (≥3.8), this should return the first mode.

Output: Mode: banana

For multiple modes use statistics.multimode:

import statistics

food = ['banana', 'banana', 'apple', 'apple']
mode = statistics.multimode(food)
print(f'Mode: {mode}')

output: Mode: ['banana', 'apple']

With strings as numbers:

from statistics import multimode, mean

numbers = ['3', '3', '4', '4']
mode = mean(map(int, multimode(numbers)))
print(f'Mode: {mode}')

Output: Mode: 3.5

Answered By: mozway

The statistics module does not work on datasets where there can be multiple "modes". This dataset is called as a bimodal dataset. You can approach the problem programmatically in the following way:

from collections import Counter
    # The Counter function from collections module helps with counting the 
    # "frequency" of items occurring inside the list
food = ['banana', 'banana', 'apple', 'apple']
data_dictionary = Counter(food)
# Now that the count of each data-item has been generated,
# The modal, bimodal or n-modal value of the data set can be decided by
# Finding the maximum count of frequencies.

max = 0 # initiating empty variable
modal_data_items = [] # initiating empty list
for key, value in data_dictionary.items():

# If the frequency-value is more that the previously recorded
# max value, then the max value is updated and the modal_values
# list gets updated to contain the data-item
  if value > max:
    max = value
    modal_data_items = [key]

    # In the case where, there are multiple modes in the data, 
    # there is only a need to append more data-items (key)
    # into the list of modal-items
  elif value == max:
print("The modes of the given data-set are:", modal_data_items)

Hope this helps!

Answered By: Ritesh Koushik

You can first use collections.Counter to count the number of occurrences of the elements, then use list comprehension to get the elements of the mode.

from collections import Counter
import statistics

result_dict = Counter(numbers)

result = [float(i) for i in result_dict.keys() if result_dict[i] == max(result_dict.values())]

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.