I'm using statistics.mode([1, 1, 2, 2, 3])
to find the mode, but I get:
no unique mode; found 2 equally common values
When more than one mode is found, how can I output either 1
or 2
?
Note that in Python 3.8
the behaviour of statistics.mode
has changed:
Changed in version 3.8: Now handles multimodal datasets by returning the first mode encountered. Formerly, it raised StatisticsError when more than one mode was found.
In your example:
from statistics import mode
mode([1, 1, 2, 2, 3])
# 1
Also starting in Python 3.8
, you can alternatively use statistics.multimode
to return the list of the most frequently occurring values in the order they were first encountered:
from statistics import multimode
multimode([1, 1, 2, 2, 3])
# [1, 2]
from scipy import stats as s
a=[1,1,2,2,3]
print(int(s.mode(a)[0]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With