0
Statistics module
calculating mode using the statistics module in python gives me an error when I use this line of code md = statistics.mode (sample). I can find the mean, variance, etc in a similar way without any issues. any ideas as to why this is failing?
6 Answers
+ 4
Nonduduzo That is because there is no *unique* value which is the most frequent, in your list.
The mode() method does not handle such situation and raises the StatisticsError. You can handle it with a try/except clause, like print out "There is no unique mode here." for example.
+ 1
Nonduduzo which Python version do you use? Perhaps it has a different implementation of the method, might be...
Just for reference:
https://docs.python.org/3.4/library/statistics.html#statistics.mode
0
Full Gamer my code is public.
0
Kuba SiekierzyĆski I used the same set of values on my PC and it works but when I try it here on Sololearn it fails
0
Full Gamer
import statistics
sample = [1,2,3,1,4,1,5,4,3,8,4,10,3]
#how come line 5 generates an error?
md = statistics.mode(sample)
med = statistics.median(sample)
var = statistics.variance(sample)
dev = statistics.stdev(sample)
print("Median = ",med)
print("Variance = ",round(var))
print("Standard deviation = ",round(dev))
- 1
I am using python 3