Statistics module | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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
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.
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