Skip to content

Commit b929b19

Browse files
committed
feat: Adding Simple Frequency Table
1 parent a3f7bca commit b929b19

File tree

2 files changed

+95
-35
lines changed

2 files changed

+95
-35
lines changed

FrequencyTable.py

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ def __init__(self, dataset):
3636
# Formula for Standard Deviation
3737
self.deviation = (self.variance ** 0.5)
3838

39-
40-
39+
# Formula to find Dataset Skewness
40+
self.skewness = (self.length / ((self.length - 1) * (self.length - 2))) * sum(((x - self.mean) / self.deviation) ** 3 for x in self.dataset)
41+
42+
# Formula to find Dataset
43+
self.kurtosis = (self.length * (self.length + 1) * sum(((x - self.mean) / self.deviation) ** 4 for x in self.dataset) / ((self.length - 1) * (self.length - 2) * (self.length - 3))) - \
44+
(3 * (self.length - 1) ** 2) / ((self.length - 2) * (self.length - 3))
45+
4146
# Populate Grouped Table Frequency Data Method
4247
def PopulateGrouped(self):
4348
# Initiating Used List
@@ -72,8 +77,8 @@ def PopulateGrouped(self):
7277
top.append(current_number)
7378

7479
# Append Class Bottom Limit
75-
current_bot_limit = old_number - 0.5
76-
bottom_limit.append(current_bot_limit)
80+
current_bottom_limit = old_number - 0.5
81+
bottom_limit.append(current_bottom_limit)
7782

7883
# Append Class Top Limit
7984
current_top_limit = current_number + 0.5
@@ -88,7 +93,7 @@ def PopulateGrouped(self):
8893
data_range.append(current_data_range)
8994

9095
# Adding Data Range Limit Of The Class Frequency
91-
current_data_limit = f"{current_bot_limit} ~ {current_top_limit}"
96+
current_data_limit = f"{current_bottom_limit} ~ {current_top_limit}"
9297
data_limit.append(current_data_limit)
9398

9499
# Adding Data Midpoint of The Class Frequency
@@ -99,7 +104,7 @@ def PopulateGrouped(self):
99104
current_bot_cumulative_frequency = self.find_frequency(self.lowest, old_number)
100105
bot_cumulative_frequency.append(current_bot_cumulative_frequency)
101106

102-
# Adding Bottom Cumulative Frequency of The Class
107+
# Adding Top Cumulative Frequency of The Class
103108
current_top_cumulative_frequency = self.find_frequency(old_number, self.highest)
104109
top_cumulative_frequency.append(current_top_cumulative_frequency)
105110

@@ -112,25 +117,72 @@ def PopulateGrouped(self):
112117
mode_index = [i for i, val in enumerate(frequency) if val == max(frequency)]
113118
mode = [data_range[i] for i in mode_index]
114119

115-
# Formula to find Dataset Skewness
116-
skewness = (self.length / ((self.length - 1) * (self.length - 2))) * sum(((x - self.mean) / self.deviation) ** 3 for x in self.dataset)
117-
118-
# Formula to find Dataset
119-
kurtosis = (self.length * (self.length + 1) * sum(((x - self.mean) / self.deviation) ** 4 for x in self.dataset) / ((self.length - 1) * (self.length - 2) * (self.length - 3))) - \
120-
(3 * (self.length - 1) ** 2) / ((self.length - 2) * (self.length - 3))
121-
122120
# Append Processed Data into Data Attributes
123-
self.grouped = ProcessedData(bottom, top, bottom_limit, top_limit,
121+
self.grouped = ProcessedData(None, bottom, top, bottom_limit, top_limit,
124122
frequency, data_range, data_limit, data_midpoint,
125123
bot_cumulative_frequency, top_cumulative_frequency,
126-
relative_frequency, skewness, kurtosis, mode)
124+
relative_frequency, mode)
125+
126+
def PopulateSimple(self):
127+
# Deleting Duplicate and Sort the Data
128+
data = sorted(set(self.dataset))
129+
130+
# Initiating Used Variable
131+
top_limit = []
132+
bottom_limit = []
133+
frequency = []
134+
top_cumulative_frequency = []
135+
bot_cumulative_frequency = []
136+
relative_frequency = []
137+
mode = []
138+
139+
for current_class in data:
140+
# Bottom Limit of the Class
141+
current_top_limit = current_class + 0.5
142+
current_bottom_limit = current_class - 0.5
127143

144+
# Top Limit of the Class
145+
top_limit.append(current_top_limit)
146+
bottom_limit.append(current_bottom_limit)
147+
148+
# Calculate Current Class Frequency
149+
current_frequency = self.dataset.count(current_class)
150+
frequency.append(current_frequency)
151+
152+
# Calculate Current Class Bottom Cumulative Frequency
153+
current_bot_cumulative_frequency = self.find_frequency(self.lowest, current_class)
154+
bot_cumulative_frequency.append(current_bot_cumulative_frequency)
155+
156+
# Calculate Current Class Top Cumulative Frequency
157+
current_top_cumulative_frequency = self.find_frequency(current_class, self.highest)
158+
top_cumulative_frequency.append(current_top_cumulative_frequency)
159+
160+
# Calculate Current Class Relative Frequency
161+
current_relative_frequency = np.round((current_frequency / self.length) * 100)
162+
relative_frequency.append(current_relative_frequency)
163+
164+
# Temukan modus
165+
mode_index = [i for i, val in enumerate(frequency) if val == max(frequency)]
166+
mode = [data[i] for i in mode_index]
167+
168+
# Buat objek ProcessedData
169+
self.simple = ProcessedData(data, None, None, bottom_limit, top_limit,
170+
frequency, None, None, None,
171+
bot_cumulative_frequency, top_cumulative_frequency,
172+
relative_frequency, mode)
173+
128174
# Base 5 Rounding
129175
def roundy(self, x, base = 5):
130176
return base * round(x/base)
131177

132178
# Function To Find Frequency in Dataset with Desired Range (Top and Down Limit)
133179
def find_frequency(self, bot, top):
180+
try:
181+
bot = int(bot)
182+
top = int(top)
183+
except (ValueError, TypeError) as e:
184+
print(f"Error converting to int: {e}")
185+
134186
total_frequency = 0
135187
for i in range(bot, top + 1):
136188
frequency = self.dataset.count(i)
@@ -140,7 +192,8 @@ def find_frequency(self, bot, top):
140192
# Processed Data Assignment
141193
class ProcessedData:
142194
# Limit (L), Frequency (F), Ranges (R), Midpoint (M), Cumulative (C), Relative (R)
143-
def __init__(self, bot, top, bot_L, top_L, F, R, L, M, bot_CF, top_CF, RF, skew, kurt, mode):
195+
def __init__(self, data, bot, top, bot_L, top_L, F, R, L, M, bot_CF, top_CF, RF, mode):
196+
self.classval = data
144197
self.bottom = bot
145198
self.top = top
146199
self.bottom_limit = bot_L
@@ -155,8 +208,6 @@ def __init__(self, bot, top, bot_L, top_L, F, R, L, M, bot_CF, top_CF, RF, skew,
155208
self.relative_frequency = RF
156209

157210
self.percentage_relative_frequency = [ f"{rf * 1:.2f}%" for rf in self.relative_frequency ]
158-
self.skewness = skew
159-
self.kurtosis = kurt
160211
self.mode = mode
161212

162213

Main.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,45 @@
44
import tabulate as tabulate
55

66
# Raw Data
7-
dataset = (
8-
58, 67, 45, 89, 72, 60, 76, 93,
9-
55, 48, 62, 85, 79, 56, 41, 90,
10-
77, 54, 68, 82, 46, 73, 57, 92,
11-
81, 53, 66, 74, 64, 52, 91, 78,
12-
49, 87, 88, 50, 69, 84, 43, 65,
13-
83, 70, 44, 61, 75, 80, 71, 63, 47,51)
7+
dataset = (1,1,1,4,6,7,3,6,7,1,2,2,5,3,1,8,3,2)
148

159
# Initiate Object From The Raw Data
1610
data = ft.FrequencyTable(dataset)
1711

1812
# Processing Raw Data to Frequency Grouped Frequency Table
19-
data.PopulateGrouped()
13+
data.PopulateSimple()
2014

2115
# Transform The Data To A Frequency Table
2216
# Initiating The Data Using Pandas
17+
# df = pd.DataFrame(
18+
# {
19+
# "Class Interval" : data.grouped.ranges,
20+
# "Class Limit" : data.grouped.limit,
21+
# "Frequency" : data.grouped.frequency,
22+
# "Midpoint" : data.grouped.midpoint,
23+
24+
# "C <" : data.grouped.bottom_limit,
25+
# "CF <" : data.grouped.bottom_cumulative_frequency,
26+
# "C >" : data.grouped.top_limit,
27+
# "CF >" : data.grouped.top_cumulative_frequency,
28+
# "Relative Frequency" : data.grouped.percentage_relative_frequency
29+
# }
30+
# )
31+
2332
df = pd.DataFrame(
2433
{
25-
"Class Interval" : data.grouped.ranges,
26-
"Class Limit" : data.grouped.limit,
27-
"Frequency" : data.grouped.frequency,
28-
"Midpoint" : data.grouped.midpoint,
34+
"Class" : data.simple.classval,
35+
"Frequency" : data.simple.frequency,
2936

30-
"C <" : data.grouped.bottom_limit,
31-
"CF <" : data.grouped.bottom_cumulative_frequency,
32-
"C >" : data.grouped.top_limit,
33-
"CF >" : data.grouped.top_cumulative_frequency,
34-
"Relative Frequency" : data.grouped.percentage_relative_frequency
37+
"C <" : data.simple.bottom_limit,
38+
"CF <" : data.simple.bottom_cumulative_frequency,
39+
"C >" : data.simple.top_limit,
40+
"CF >" : data.simple.top_cumulative_frequency,
41+
"Relative Frequency" : data.simple.percentage_relative_frequency
3542
}
3643
)
3744

45+
3846
# Converting Pandas Data Into Tabulate
3947
table = tabulate.tabulate(
4048
df,
@@ -45,3 +53,4 @@
4553
# Print Output Data
4654
print(table)
4755

56+

0 commit comments

Comments
 (0)