from statistics import mode
from statistics import multimode
import scipy.stats
import numpy as np
# Importing fractions module as fr Enables to calculate harmonic_mean of a set in Fraction
from fractions import Fraction as fr

######################## using pure python ######################################

u = [2, 3, 2, 8, 12]
mode_ = max((u.count(item), item) for item in set(u))[1]
print("Mode using pure python: ", mode_)

################### using statistics ##############################
 
# tuple of positive integer numbers
data1 = (2, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 7)
print("Mode of data set 1 is % s" % (multimode(data1)))
print("Mode of data set 1 is % s" % (mode(data1)))

#no mode example Note:the function mode() returns the first one encountered in the data
data1 = (2, 2, 4, 1, 3, 4)
print("Mode of data set 2 is % s" % (multimode(data1)))
print("Mode of data set 2 is % s" % (mode(data1)))

# tuple of a set of floating point values
data2 = (2.4, 1.3, 1.3, 1.3, 2.4, 4.6)
print("Mode of data set 3 is % s" % (mode(data2)))

# tuple of a set of fractional numbers
data3 = (fr(1, 2), fr(1, 2), fr(10, 3), fr(2, 3))
print("Mode of data set 4 is % s" % (mode(data3))) 

# tuple of a set of negative integers
data4 = (-1, -2, -2, -2, -7, -7, -9)
print("Mode of data set 5 is % s" % (mode(data4))) 

# tuple of strings
data5 = ("red", "blue", "black", "blue", "Black", "black", "brown")
print("Mode of data set 6 is % s" % (mode(data5)))

############################ using scipy.stats.mode() ############################

v = [12, 15, 12, 15, 21, 15, 12]
v = np.array(v)
mode_ = scipy.stats.mode(v)
print("mode using scipy.stats.mode(): ", mode_[1])
#Note: If there are multiple modal values in the dataset, then only the smallest value is returned