Skip to content

Commit cb20062

Browse files
committed
fix: Returning Missing Simple Frequency Table Method
1 parent 3b95bc3 commit cb20062

File tree

2 files changed

+118
-45
lines changed

2 files changed

+118
-45
lines changed

FrequencyTable.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,75 @@ def PopulateGrouped(self):
152152
frequency, data_range, data_limit, data_midpoint,
153153
bot_cumulative_frequency, top_cumulative_frequency,
154154
relative_frequency, mode)
155+
156+
# Populate Simple Table Frequency Data Method
157+
def PopulateSimple(self):
158+
# Initialize general variables
159+
data = sorted(set(self.dataset)) # Remove duplicates and sort the data
160+
frequency = [] # To store the frequency of each class
161+
top_cumulative_frequency = [] # To store top cumulative frequency for each class
162+
bot_cumulative_frequency = [] # To store bottom cumulative frequency for each class
163+
relative_frequency = [] # To store relative frequency for each class
164+
mode = [] # To store the mode(s)
165+
166+
# Variables specifically for numeric data
167+
top_limit = None
168+
bottom_limit = None
169+
170+
# Check if the dataset is not entirely string-based (for numeric data)
171+
if not all(isinstance(item, str) for item in self.dataset):
172+
# Initialize limits for numeric data
173+
top_limit = []
174+
bottom_limit = []
175+
176+
# Single loop to process both numeric and string data
177+
for current_class in data:
178+
# Calculate the frequency of the current class
179+
current_frequency = self.dataset.count(current_class)
180+
frequency.append(current_frequency)
181+
182+
# Calculate the relative frequency for the current class
183+
current_relative_frequency = np.round((current_frequency / self.length) * 100)
184+
relative_frequency.append(current_relative_frequency)
185+
186+
# If the data is numeric, calculate limits and cumulative frequencies
187+
if top_limit is not None and bottom_limit is not None:
188+
# Calculate top and bottom limits for numeric data
189+
current_top_limit = current_class + 0.5
190+
current_bottom_limit = current_class - 0.5
191+
top_limit.append(current_top_limit)
192+
bottom_limit.append(current_bottom_limit)
193+
194+
# Calculate bottom cumulative frequency for numeric data
195+
current_bot_cumulative_frequency = self.find_frequency(self.lowest - 1, current_class)
196+
bot_cumulative_frequency.append(current_bot_cumulative_frequency)
197+
198+
# Calculate top cumulative frequency for numeric data
199+
current_top_cumulative_frequency = self.find_frequency(current_class + 1, self.highest + 1)
200+
top_cumulative_frequency.append(current_top_cumulative_frequency)
201+
202+
else:
203+
# If the data is string-based, calculate cumulative frequencies
204+
# Calculate bottom cumulative frequency for strings
205+
current_bot_cumulative_frequency = self.dataset.count(current_class)
206+
bot_cumulative_frequency.append(current_bot_cumulative_frequency)
207+
208+
# Calculate top cumulative frequency for strings
209+
current_top_cumulative_frequency = sum(frequency) - current_bot_cumulative_frequency
210+
top_cumulative_frequency.append(current_top_cumulative_frequency)
211+
212+
# Find the mode (the class with the highest frequency)
213+
mode_index = [i for i, val in enumerate(frequency) if val == max(frequency)]
214+
mode = [data[i] for i in mode_index]
215+
216+
# Create the ProcessedData object based on the data type
217+
self.simple = ProcessedData(
218+
data, None, None, bottom_limit, top_limit,
219+
frequency, None, None, None,
220+
bot_cumulative_frequency, top_cumulative_frequency,
221+
relative_frequency, mode
222+
)
223+
155224

156225
# Processed Data Assignment
157226
class ProcessedData:

Main.py

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,71 @@
44
import tabulate as tabulate
55

66
# Raw Data
7-
dataset = [12.5, 43.2, 56.7, 12.1, 98.3, 34.2, 78.4, 67.9, 23.5, 45.6,
8-
78.1, 89.0, 32.4, 56.8, 44.5, 77.2, 12.6, 35.8, 67.1, 23.3,
9-
56.5, 78.9, 99.5, 22.4, 10.2, 35.1, 48.6, 59.9, 71.3, 84.2,
10-
45.3, 67.8, 89.1, 33.3, 76.4, 88.7, 41.2, 12.7, 34.4, 67.4,
11-
23.8, 55.1, 77.3, 90.4, 13.5, 14.6, 55.7, 22.2, 33.1, 66.5,
12-
78.2, 39.5, 41.8, 91.2, 12.4, 64.7, 49.9, 80.5, 92.3, 38.8,
13-
14.5, 99.1, 25.4, 26.8, 37.5, 52.3, 43.8, 76.8, 28.7, 64.8,
14-
14.9, 15.3, 48.5, 82.2, 93.4, 56.3, 88.3, 60.5, 72.9, 38.3,
15-
57.2, 70.1, 84.4, 97.2, 18.6, 45.1, 66.1, 31.9, 94.5, 29.4,
16-
11.9, 16.7, 21.1, 88.9, 99.7, 53.6, 62.0, 34.9, 82.8, 18.9,]
7+
dataset = [
8+
'Mango', 'Pineapple', 'Banana', 'Banana', 'Pineapple', 'Banana',
9+
'Banana', 'Grapes', 'Pear', 'Pineapple', 'Orange', 'Strawberry',
10+
'Orange', 'Mango', 'Banana', 'Pineapple', 'Orange', 'Banana',
11+
'Strawberry', 'Pear', 'Apple', 'Banana', 'Pineapple', 'Orange',
12+
'Mango', 'Apple', 'Pear', 'Pear', 'Pear', 'Grapes', 'Pear',
13+
'Orange', 'Grapes', 'Strawberry', 'Mango', 'Orange', 'Orange',
14+
'Mango', 'Pear', 'Strawberry', 'Pear', 'Orange', 'Mango',
15+
'Mango', 'Pear', 'Grapes', 'Apple', 'Mango', 'Pineapple',
16+
'Strawberry', 'Strawberry', 'Grapes', 'Apple', 'Banana',
17+
'Grapes', 'Banana', 'Strawberry', 'Mango', 'Strawberry',
18+
'Orange', 'Pear', 'Grapes', 'Orange', 'Apple'
19+
]
1720

1821

1922
# Initiate Object From The Raw Data
2023
data = ft.FrequencyTable(dataset)
2124

2225
# Processing Raw Data to Frequency Grouped Frequency Table
23-
data.PopulateGrouped() # Grouped Data
24-
# data.PopulateSimple() # Simple Data
26+
# data.PopulateGrouped() # Grouped Data
27+
data.PopulateSimple() # Simple Data
2528

2629
# Transform The Data To A Frequency Table
2730
# Initiating The Data Using Pandas
2831
# Grouped Populated Data
29-
dfg = pd.DataFrame(
30-
{
31-
"Class Interval" : data.grouped.ranges,
32-
"Class Limit" : data.grouped.limit,
33-
"Frequency" : data.grouped.frequency,
34-
"Midpoint" : data.grouped.midpoint,
35-
36-
"C <" : data.grouped.bottom_limit,
37-
"CF <" : data.grouped.bottom_cumulative_frequency,
38-
"C >" : data.grouped.top_limit,
39-
"CF >" : data.grouped.top_cumulative_frequency,
40-
"Relative Frequency" : data.grouped.percentage_relative_frequency
41-
}
42-
)
43-
44-
# Simple Populated Data
45-
# dfs = pd.DataFrame(
32+
# dfg = pd.DataFrame(
4633
# {
47-
# "Class" : data.simple.classval,
48-
# "Frequency" : data.simple.frequency,
49-
# "Relative Frequency" : data.simple.percentage_relative_frequency
34+
# "Class Interval" : data.grouped.ranges,
35+
# "Class Limit" : data.grouped.limit,
36+
# "Frequency" : data.grouped.frequency,
37+
# "Midpoint" : data.grouped.midpoint,
38+
39+
# "C <" : data.grouped.bottom_limit,
40+
# "CF <" : data.grouped.bottom_cumulative_frequency,
41+
# "C >" : data.grouped.top_limit,
42+
# "CF >" : data.grouped.top_cumulative_frequency,
43+
# "Relative Frequency" : data.grouped.percentage_relative_frequency
5044
# }
5145
# )
5246

53-
# Converting Pandas Data Into Tabulate
54-
# tablesimple = tabulate.tabulate(
55-
# dfs,
56-
# headers='keys',
57-
# tablefmt='pipe'
58-
# )
47+
# Simple Populated Data
48+
dfs = pd.DataFrame(
49+
{
50+
"Class" : data.simple.classval,
51+
"Frequency" : data.simple.frequency,
52+
"Relative Frequency" : data.simple.percentage_relative_frequency
53+
}
54+
)
5955

60-
tablegrouped = tabulate.tabulate(
61-
dfg,
56+
# Converting Pandas Data Into Tabulate
57+
tablesimple = tabulate.tabulate(
58+
dfs,
6259
headers='keys',
63-
tablefmt='pipe',
64-
)
60+
tablefmt='pipe'
61+
)
62+
63+
# tablegrouped = tabulate.tabulate(
64+
# dfg,
65+
# headers='keys',
66+
# tablefmt='pipe',
67+
# )
6568

6669
# Print The Processed Data
67-
# print(tablesimple)
68-
print(tablegrouped)
69-
print(data.length)
70+
print(tablesimple)
71+
# print(tablegrouped)
72+
73+
7074

0 commit comments

Comments
 (0)