Monday 21 February 2022

Mean, Median and Mode in Python (Part 2/3)

Let us now examine the Median of a dataset. We use this to discover the middle of a dataset.

The Median

How the Median is derived, is to sort the values, then take the value that lies right in the middle of the sorted dataset. If there are two values in the middle of the dataset, we take the Mean of these values as the Median.

So here's our function, tt_median().
def tt_median (vals):


We sort the list using the standard sort() method.
def tt_median (vals):
    vals_sorted = vals.sort()


Declare med. Check for the length of the list. If the length is even, make med the average of the values just before and after the halfway mark. We can use the tt_mean() function we've already written, for this.
def tt_median (vals):
    vals_sorted = vals.sort()
    med = 0
    
    if (len(vals) % 2 == 0):
        med = tt_mean([vals[int(len(vals) / 2 - 1)], vals[int(len(vals) / 2)]])
    else:


If the length of the list is odd, then just set med to the value of the element right at the midpoint of the list. Finally, return med.
def tt_median (vals):
    vals_sorted = vals.sort()
    med = 0
    
    if (len(vals) % 2 == 0):
        med = tt_mean([vals[int(len(vals) / 2 - 1)], vals[int(len(vals) / 2)]])
    else:
        med = vals[int(len(vals) / 2)]
            
    return med


Let's try this with two different lists. test will be the list we defined while testing tt_mean().
def tt_median (vals):
    vals_sorted = vals.sort()
    med = 0
    
    if (len(vals) % 2 == 0):
        med = tt_mean([vals[int(len(vals) / 2 - 1)], vals[int(len(vals) / 2)]])
    else:
        med = vals[int(len(vals) / 2)]
            
    return med

test2 = [3, 3, 10, 17, 17]

print(tt_median(test))
print(tt_median(test2))



Compare the results against NumPy's median() function.
print(tt_median(test))
print(tt_median(test2))

print(np.median(test))
print(np.median(test2))


Close enough!


Next

Last but definitely not least, the Mode.

No comments:

Post a Comment