-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_of_central_tendency.py
188 lines (105 loc) · 3.32 KB
/
measure_of_central_tendency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
__author__ = 'leasynoth'
__email__ = '[email protected]'
__DATE__ = '04.06.2022'
'''
measure of central tendency
'''
def arithmetic_mean(data):
res = 0
for elem in data:
res += elem
return res/len(data)
def geometric_mean(data):
res = 1
for elem in data:
if (elem > 0):
res *= elem
return res ** (1/len(data))
def geometric_mean2(data=[1, 2]):
res = 0
for elem in data:
res += (elem ** 0.5)
return (res / len(data)) ** (1/0.5)
def quadratic_mean(data):
res = 0
for elem in data:
res += elem ** 2
return (res / len(data)) ** (1/2)
def harmonic_mean(data):
res = 0
for elem in data:
if (elem > 0):
res += (1 / elem)
return (len(data) / res)
def cubic_mean(data):
res = 0
for elem in data:
res += elem ** 3
return (res / len(data)) ** (1/3)
def winsorized_mean(data):
res = 0
data.sort()
percent = round((len(data) / 100) * 20)
cutted_data = data[percent:len(data)-percent]
first_elem = cutted_data[0]
last_elem = cutted_data[-1]
for i in range(percent):
cutted_data.insert(0, first_elem)
cutted_data.insert(-1, last_elem)
for elem in cutted_data:
res += elem
return res / len(cutted_data)
def trimmed_mean(data):
res = 0
data.sort()
percent = round((len(data) / 100) * 20)
cutted_data = data[percent:len(data)-percent]
for elem in cutted_data:
res += elem
return res / len(cutted_data)
def mediana(data):
res = 0
data.sort()
half = len(data) // 2
if ((len(data) % 2) == 1):
res = data[half]
elif ((len(data) % 2) == 0):
res = (data[half-1] + data[half]) / 2
return res
def moda(data):
res = 0
summary_dict = {}
for elem in data:
alpha = summary_dict.get(elem)
if (alpha == None):
summary_dict.update({elem:1})
elif (alpha != None):
summary_dict.update({elem:alpha+1})
res = max(summary_dict, key=summary_dict.get)
return res
def main():
test_data = [_ for _ in range(10)]
a_m = round(arithmetic_mean(test_data), 1)
g_m = round(geometric_mean(test_data), 1)
g_m2 = round(geometric_mean2(test_data), 1)
q_m = round(quadratic_mean(test_data), 1)
h_m = round(harmonic_mean(test_data), 1)
c_m = round(cubic_mean(test_data), 1)
w_m = round(winsorized_mean(test_data), 1)
t_m = round(trimmed_mean(test_data), 1)
med = round(mediana(test_data), 1)
mo = moda(test_data)
print('Input array: ' + str(test_data) + '\n')
print('Harmonic mean: ' + str(h_m) + '\n')
print('Geometric mean: ' + str(g_m) + '\n')
print('Geometric mean 2: ' + str(g_m2) + '\n')
print('Arithmetic mean: ' + str(a_m) + '\n')
print('Quadratic mean: ' + str(q_m) + '\n')
print('Cubic mean: ' + str(c_m) + '\n')
print('Winsorized mean: ' + str(w_m) + '\n')
print('Trimmed mean: ' + str(t_m) + '\n')
print('Mediana: ' + str(med) + '\n')
print('Moda: ' + str(mo) + '\n')
if (__name__ == '__main__'):
main()