Home > front end >  calculation of measures of descriptive statistics
calculation of measures of descriptive statistics

Time:03-16

In this program you CANNOT USE python libraries (pandas, numpy, etc), nor python functions (sum, etc). Fulfilling all this, I would like to know how I could calculate these measures of my quantitative variable: mean, median and mode.

This is the data reading of my quantitative variable.

#we enter people's salaries
def salary(n):
    L=[]
    for elem in range(n):
        print("enter the person's salary:")
        L.append(float(input()))
    return(L)

CodePudding user response:

You have to count several numbers separately and first sort the list of numbers (the following example assumes that the list of numbers you pass in is unordered)

median: just take the middle digit of the sorted list plural: distinguish between the presence or absence of a plural and the existence of multiple pluralities average: sum and divide by length, try this:

def get_sort_lst(lst):
    n = len(lst)
    for i in range(1, n):
        tmp, j = lst[i], i - 1
        while j >= 0 and lst[j] > tmp:
            lst[j   1] = lst[j]
            j -= 1
        lst[j   1] = tmp
    return lst


def get_median(lst):
    if len(lst) % 2 == 0:
        n = len(lst) // 2
        return (lst[n-1]   lst[n]) / 2
    else:
        return lst[len(lst)//2]


def get_mean(lst):
    res = 0
    for item in lst:
        res  = item
    return res / len(lst)


def get_plural(lst):
    res, plural = {}, []
    for item in lst:
        if item not in res:
            res[item] = 1
        else:
            res[item]  = 1
    for k, v in res.items():
        if not plural:
            plural.append(k)
        else:
            if v > res[plural[0]]:
                plural = [k]

            elif k not in plural and v == res[plural[0]]:
                plural.append(k)
    if res[plural[0]] == 1:
        return "No plural"
    else:
        return plural


def salary(lst):
    lst = get_sort_lst(lst)
    print("Mean: {}, Median: {}, Plural: {}".format(get_mean(lst), get_median(lst), get_plural(lst)))


salary([1, 2, 3, 4, 5, 5])

CodePudding user response:

You may try something like this

total = 0
count = 0
for i in L:
    total  = i
    count  = 1

Mean
mean = total/count

Median
median = L[count//2]

You can see this post to calculate mode

  • Related