๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ซ ์ˆ˜ํ•™

Day 0: mean, median, mode, weighted mean


HackerRank 10 days of statistics

: Day 0 note taking




This is just for practice, can make it much shorter with some packages like numpy, scipy.

array = [n1, n2 .... nN]

type(n) == int

N = len(array)



Mean

sum( all N element in array) / N



Median: middle element in a sorted array. 

If a number of N is even, then you have two elements in the middle of the array.

an average of those two elements is median.


if N%2 == 0:

median = (arr[int(N/2)-1]+arr[int(N/2)])/2

else:

median = arr[int(N/2)]



Mode: the most popular number

    It means the element who have the highest frequency of occurrence in the array.


    arr_set = sorted(set(arr))

    mode = arr[0]

    count = 0


    for element in arr_set:

        tmp = arr.count(element)

        if  tmp > count:

            mode = element

            count = tmp





xxx = [ an array of discrete numbes, x ]

www = [ an array of corresponding weights, w ]


N = number of elements in the arrays  (# of x_arr elements == # of w_arr elements)


Weighted Mean: all sum(x, w) in the array /sum(w_arr)


xw = sum([x*y for x,y in zip(xxx,www)])

weighted_mean = (round(xw/sum(www),1))