python mode, average, median implementation code (including the use of Dictionary)

Instead of numpy, use python to judge the median and average of modes in a list

Title:
Statisticians want to use a set of functions to determine the median and mode of a list of numbers. The median is the number that appears in the middle after sorting a list. Mode is the number that appears most frequently in the list. These functions are defined in a module. It also includes a function called mean, which calculates the average of a set of numbers. Each function takes a list of numbers as an argument and returns a single number.

Idea:
Define a class, and then write a sorting function to calculate the average function, the median function and the mode function.

Sort:
Bubble sorting;

    def sorting(self, list=[]):
        for i in range(len(list)):
            for j in range(len(list) - 1 - i):
                if list[j] > list[j + 1]:
                    list[j], list[j + 1] = list[j + 1], list[j]
        return list

median:
After the list is passed in, initialize and execute the sorting function to get a list sorted from small to large, so the median is the value in the middle of the label. The length of the list is always an integer, and the characteristics of parity need to be considered.

    def median(self,list = []):
        n = len(list)
        if (n% 2) == 0:
            local = int(n/2)
            # The data type after division is float
        else:
            local = int((n+1)/2)
        return list[local]

average:
Add all of the list and divide by the length of the list

    def mean(self,list = []):
        sum = 0
        for i in range(len(list)):
            sum += list[i]
        means = sum/len(list)
        return means

Mode:
The api implementation of numpy is not used to find the mode.
The main idea is to traverse the elements in the list, check the occurrence times of each element, and create a dictionary. The content of the dictionary is the corresponding occurrence times of elements and elements.
Use the max function to find the largest value in value.
However, there may be two elements in the list with the same number of occurrences and the largest number of occurrences. In this case, several elements should be filtered out, and the direct use of max function and corresponding key value search can only find the content that appears first in the traversal process. Therefore, traverse the dictionary to find each element with the most occurrences.

    def mode(self,lists = []):
        dic = {}
        k = 1
        for i in range(len(lists)):
            for j in range(len(lists)):
                if lists[j] == lists[j-1]:
                    k += 1
                else:
                    dic[lists[j-1]] = k
                    k = 1
        value = max(dic.values())
        max_list = []
        for m, n in dic.items():  # Traverse the dictionary to find the corresponding key value
            print(m,n)
            if n == value:
                max_list.append(m)
        return value,max_list

All codes

class Calculate(object):
    def __init__(self,lists = []):
        self.sorting(lists)

    def sorting(self, list=[]):
        for i in range(len(list)):
            for j in range(len(list) - 1 - i):
                if list[j] > list[j + 1]:
                    list[j], list[j + 1] = list[j + 1], list[j]
        return list

    nums = 0
    def median(self,list = []):
        n = len(list)
        if (n% 2) == 0:
            local = int(n/2)
        else:
            local = int((n+1)/2)
        return list[local]

    def mean(self,list = []):
        sum = 0
        for i in range(len(list)):
            sum += list[i]
        means = sum/len(list)
        return means

    def mode(self,lists = []):
        dic = {}
        k = 1
        for i in range(len(lists)):
            for j in range(len(lists)):
                if lists[j] == lists[j-1]:
                    k += 1
                else:
                    dic[lists[j-1]] = k
                    k = 1
        print(dic)
        value = max(dic.values())
        max_list = []
        for m, n in dic.items():  # Traverse the dictionary to find the corresponding key value
            print(m,n)
            if n == value:
                max_list.append(m)
        return value,max_list

Test code 1:

s = Calculate()
lists = [0,1,2,5,6,9,7,8,6,3,1,1,4,2,2,7,8,9,5,6,8,6,4,9,9,9,9,9,6,6,6]

new_list = s.sorting(lists)
print(new_list)
means = s.mean(lists)
print(means)
medians = s.median(lists)
print(medians)
modes = s.mode(lists)
print(modes)

Result 1:

[0, 1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9]
5.580645161290323
6
(7, [9, 6])

Test code 2:

s = Calculate()
lists = [1,5,6,8,8,8,7,7,7,9,9,9,4,4,0,0,5,5,4]

new_list = s.sorting(lists)
print(new_list)
means = s.mean(lists)
print(means)
medians = s.median(lists)
print(medians)
modes = s.mode(lists)
print(modes)

Result 2:

[0, 0, 1, 4, 4, 4, 5, 5, 5, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9]
5.578947368421052
7
(3, [9, 4, 5, 7, 8])

Keywords: Python data structure

Added by xdentan on Wed, 09 Mar 2022 12:18:08 +0200