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?

12th Jun 2018, 7:32 PM
Nonduduzo
Nonduduzo - avatar
7 Respuestas
+ 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.
12th Jun 2018, 8:35 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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
12th Jun 2018, 8:54 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Full Gamer my code is public.
12th Jun 2018, 8:44 PM
Nonduduzo
Nonduduzo - avatar
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
12th Jun 2018, 8:45 PM
Nonduduzo
Nonduduzo - avatar
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))
12th Jun 2018, 8:53 PM
Nonduduzo
Nonduduzo - avatar
- 1
I am using python 3
14th Jun 2018, 9:03 AM
Nonduduzo
Nonduduzo - avatar