# Day 0 - Median, mean, mode and weighted mean - Blog
Data Science for Political and Social Phenomena
Author: Guillaume Redoulès

# Day 0 - Median, mean, mode and weighted mean

## A reminder

### The median

The median is the value separating the higher half from the lower half of a data sample. For a data set, it may be thought of as the middle value. For a continuous probability distribution, the median is the value such that a number is equally likely to fall above or below it.

### The mean

The arithmetic mean (or simply mean) of a sample is the sum of the sampled values divided by the number of items.

### The mode

The mode of a set of data values is the value that appears most often. It is the value x at which its probability mass function takes its maximum value. In other words, it is the value that is most likely to be sampled.

## Implementation in python without using the scientific libraries

    def median(l):
        l = sorted(l)
        if len(l) % 2 == 0:
            return (l[len(l) // 2] + l[(len(l)//2 - 1)]) / 2
        else:
            return l[len(l)//2]

    def mean(l):
        return sum(l)/len(l)

    def mode(data):
        dico = {x:data.count(x) for x in list(set(data))}
        return sorted(sorted(dico.items()), key = lambda x: x[1], reverse = True)[0][0]

    L = [64630,11735,14216,99233,14470,4978,73429,38120,51135,67060, 4978, 73429]
    print(f"Sample : {L}\nMean : {mean(L)}, Median : {median(L)}, Mode : {mode(L)}")

    Sample : [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060, 4978, 73429]
    Mean : 43117.75, Median : 44627.5, Mode : 4978

## The weighted average

The weighted arithmetic mean is similar to an ordinary arithmetic mean (the most common type of average), except that instead of each of the data points contributing equally to the final average, some data points contribute more than others.

    data = [10,40,30,50,20]
    weights = [1,2,3,4,5]
    sum_X = sum([x*w for x,w in zip(data,weights)])
    print(round((sum_X/sum(weights)),1))

    32.0

### Find an error or bug? Have a suggestion?

Everything on this site is avaliable on GitHub. Head on over and submit an issue. You can also message me directly by email.