Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No unique mode; found 2 equally common values

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?

like image 705
mgcy Avatar asked Jun 21 '18 02:06

mgcy


2 Answers

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]
like image 136
Xavier Guihot Avatar answered Nov 02 '22 20:11

Xavier Guihot


from scipy import stats as s
a=[1,1,2,2,3]
print(int(s.mode(a)[0]))
like image 39
Sai Akshay Erupaka Avatar answered Nov 02 '22 20:11

Sai Akshay Erupaka