It's been quite a while since I last had to calculate the mean, median or mode of any set of numbers. But here I am, giving myself a refresher on statistical calculations. I'm learning some basics with the help of W3 schools. They always break concepts down so well :)
Calculating Mean
We can do this the hard way, which sucks and I'm lazy and is ridiculous when we're looking at incredible large sets of numbers anyway. So instead, we're going to do it the easy way using the NumPy module in Python.
import numpy
numSet = [ *lots of numbers *]
numSetMean = numpy.mean(numSet)
Calculating Median
This is even more annoying to calculate manually. You have to sort all the values from smallest to largest and search for the value in the middle. No one has time for that. Numpy can do this too.
import numpy
numSet = [ *lots of numbers* ]
numSetMedian = numpy.median(numSet)
Mode
This is just as troublesome to calculate as the median. You're trying to get the value that appears with the highest frequency. Meh. You have to use SciPy for this though.
from scipy import stats
numSet = [ *lots of numbers* ]
numSetMode = stats.mode(numSet)
Comments
Post a Comment