-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatistics_continuous_data.py
More file actions
162 lines (119 loc) · 3.81 KB
/
Statistics_continuous_data.py
File metadata and controls
162 lines (119 loc) · 3.81 KB
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
from functools import reduce
classInterval = ["0-50", "50-100", "100-150", "150-200", "200-250"]
frequency = [1, 5, 6, 3, 80]
# i^th position calculator
def locator(i):
pos_i = pos(i, c_f((frequency))[-1])
c_cf = list(filter(lambda x: x >= pos_i, c_f(frequency)))
helper = classInterval[c_f(frequency).index(c_cf[0])].split("-")
l = int(helper[0])
f = frequency[c_f(frequency).index(c_cf[0])]
p_cf = c_f(frequency)[(frequency.index(f) - 1)]
i = int(helper[1]) - l
return pos_i, l, f, p_cf, i
pos = lambda i, n: i * (n / 4)
def middle_value(x):
m = []
helper = []
init = 0
nxt = 1
for item in x:
classs = item.split("-")
for i in classs:
helper.append(int(i))
for i in range(len(helper)):
if nxt < len(helper):
avg = lambda a, b: (a + b) / 2
average = avg(helper[init], helper[nxt])
init += 2
nxt += 2
m.append(average)
return m
# c.f
def c_f(freq):
cnt = 0
_cf = []
_cf.append(freq[0])
for i in range(1, len(freq)):
if cnt < len(freq):
_cf.append(_cf[cnt] + freq[i])
cnt += 1
return _cf
# fm
def _fm(f, m):
_fm = []
for i in range(len(f)):
fm = f[i] * m[i]
_fm.append(fm)
return _fm
# mean
operation = int(
input(
"\nPlease select an operation\n1.Mean\n2.Upper quartile(Q3)\n3.Lower quartile(Q1)\n4.Median/Q2\n5.Mode\n>> "
)
)
if operation == 1:
def mean(f):
m = middle_value(x=classInterval)
fm = _fm(f=f, m=m)
n = c_f(frequency)[-1]
summessionFM = reduce(lambda x, y: x + y, fm)
return summessionFM / n
print(f"x: {classInterval}")
print(f"f: {frequency}")
mv = middle_value(classInterval)
print(f"m: {mv}")
fm = _fm(frequency, mv)
print(f"fm: {fm}")
mean = mean(f=frequency)
print(f"Mean: {mean}")
elif operation == 2:
# Q3
def Upper_quartile():
pos_i, l, f, p_cf, i = locator(3)
print(
f"\nClass-Interval(x): {classInterval}\nFrequency (f): {frequency}\nPosition of (Q3): {pos_i}\nl: {l} f: {f} c.f: {p_cf} i: {i}\n"
)
return l + ((pos_i - p_cf) / f) * i
Q3 = Upper_quartile()
print(f"Upper Quartile (Q3): {Q3}")
elif operation == 3:
# Q1
def Lower_quartile():
pos_i, l, f, p_cf, i = locator(1)
print(
f"\nClass-Interval(x): {classInterval}\nFrequency (f): {frequency}\nPosition of (Q1): {pos_i}\nl: {l} f: {f} c.f: {p_cf} i: {i}\n"
)
return l + ((pos_i - p_cf) / f) * i
Q1 = Lower_quartile()
print(f"Lower Quartile (Q1): {Q1}")
elif operation == 4:
# median or Q2
def median():
pos_i, l, f, p_cf, i = locator(2)
print(
f"\nClass-Interval(x): {classInterval}\nFrequency (f): {frequency}\nPosition of median or (Q2): {pos_i}\nl: {l} f: {f} c.f: {p_cf} i: {i}\n"
)
return l + ((pos_i - p_cf) / f) * i
md = median()
print(f"Median(Md) or 2nd Quartile (Q2): {md}")
elif operation == 5:
# mode
def mode(f):
f1 = max(f)
f1_index = f.index(f1)
helper = classInterval[f1_index].split("-")
l = int(helper[0])
f0 = f[f1_index - 1]
f2 = 0
i = int(helper[1]) - l
try:
f2 = f2 + f[f1_index + 1]
except:
f2 = f2 + 0
print(
f"\nClass-Interval(x): {classInterval}\nFrequency (f): {frequency}\nl:{l} f1:{f1} f0:{f0} f2:{f2} i:{i}"
)
return l + ((f1 - f0) / (2 * f1 - f0 - f2)) * i
mo = mode(frequency)
print(f"\nMode:{mo}")