Home > Mobile >  How to find the mode of a list when there are multiple modes - Python
How to find the mode of a list when there are multiple modes - Python

Time:09-21

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

CodePudding user response:

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']

CodePudding user response:

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)
print(data_dictionary)
# 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:
    modal_data_items.append(key)
    
print("The modes of the given data-set are:", modal_data_items)

Hope this helps!

  • Related