Mode
The mode is the value that occurs most often. It is the only average that works for every type of data, including nominal categories such as colours or brands, and it always is an actual value from the dataset.
Finding the mode
Count how often each value occurs and pick the most frequent one:
from collections import Counter
import statistics
shoe_sizes = [42, 39, 41, 42, 44, 40, 42, 41, 43, 42, 38, 41]
counts = Counter(shoe_sizes)
print("counts:", dict(sorted(counts.items())))
print("most common:", counts.most_common(1))
print("statistics.mode:", statistics.mode(shoe_sizes))counts: {38: 1, 39: 1, 40: 1, 41: 3, 42: 4, 43: 1, 44: 1}
most common: [(42, 4)]
statistics.mode: 42
A shoe shop cares about the mode much more than the mean: a mean size of 41.25 isn't a shoe you can stock.
Several modes
A dataset can have more than one mode:
- unimodal: one most frequent value,
- bimodal: two values tie for most frequent,
- multimodal: more than two.
statistics.mode returns only the first mode it encounters, while statistics.multimode returns all of them:
import statistics
drinks = ["tea", "coffee", "juice", "coffee", "tea", "water", "tea", "coffee"]
print("mode :", statistics.mode(drinks))
print("multimode:", statistics.multimode(drinks))
all_different = [3, 1, 4, 5, 9]
print("no repeats:", statistics.multimode(all_different))mode : tea
multimode: ['tea', 'coffee']
no repeats: [3, 1, 4, 5, 9]
When every value occurs once, every value is a "mode", which tells you nothing. The mode is most informative when some values repeat clearly more than others.
The mode with pandas
Series.mode() always returns a Series, because there may be several modes:
import pandas as pd
orders = pd.DataFrame({
"size": ["M", "L", "M", "S", "M", "XL", "L", "M", "L"],
"colour": ["red", "blue", "blue", "blue", "red", "red", "green", "red", "blue"],
})
print("most common size :", orders["size"].mode().tolist())
print("most common colour:", orders["colour"].mode().tolist())
print()
print(orders["colour"].value_counts())most common size : ['M']
most common colour: ['blue', 'red']
colour
red 4
blue 4
green 1
Name: count, dtype: int64
Here colour is bimodal: red and blue are both ordered four times.
Modal class for continuous data
For continuous data, exact values rarely repeat, so instead of a modal value we report the modal class: the bin with the highest frequency in a grouped frequency table or histogram.
import numpy as np
rng = np.random.default_rng(6)
battery_hours = rng.normal(14.5, 1.8, size=300).round(2)
print("distinct values:", len(np.unique(battery_hours)), "of", battery_hours.size)
counts, edges = np.histogram(battery_hours, bins=np.arange(8, 21, 1))
k = counts.argmax()
print(f"modal class: [{edges[k]:.0f}, {edges[k + 1]:.0f}) hours with {counts[k]} phones")distinct values: 232 of 300
modal class: [14, 15) hours with 62 phones
With hundreds of distinct values, "the mode" of the raw numbers would be meaningless, but the modal class shows where the data is most concentrated.
Strengths and weaknesses
| Strengths | Weaknesses |
|---|---|
| Works for nominal data | May not be unique, or may not exist meaningfully |
| Always an actual data value | Ignores most of the data |
| Not affected by extreme values | Unstable in small samples: one extra value can change it |
Test yourself
What is the mode of 3, 7, 7, 2, 9, 3, 7?
- 3
- 7
- 5.43
- 9
What does statistics.multimode([1, 1, 2, 2, 3]) return?
1[1, 2][1]- An error
Which average is suitable for the variable 'country of birth'?
- Mean
- Median
- Mode
- Geometric mean