From 239a9fd8aea8dc40735c900067e8808720919920 Mon Sep 17 00:00:00 2001 From: brotherzhafif Date: Tue, 15 Oct 2024 06:42:59 +0700 Subject: [PATCH] fix: Error Processing Text Data in Simple Frequency Table Because Outbound List --- Example.py | 53 +++++++++++++++++++++++++++++------------------ FrequencyTable.py | 37 ++++++++++++++------------------- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/Example.py b/Example.py index 284fdd0..16799d1 100644 --- a/Example.py +++ b/Example.py @@ -1,5 +1,6 @@ # EXAMPLE PROGRAM import FrequencyTable as ft +import Chart as ct import pandas as pd import tabulate as tabulate @@ -22,26 +23,26 @@ data = ft.FrequencyTable(dataset) # Processing Raw Data to Frequency Grouped Frequency Table -data.PopulateGrouped() # Grouped Data +# data.PopulateGrouped() # Grouped Data data.PopulateSimple() # Simple Data # Transform The Data To A Frequency Table # Initiating The Data Using Pandas # Grouped Populated Data -dfg = pd.DataFrame( - { - "Class Interval" : data.grouped.ranges, - "Class Limit" : data.grouped.limit, - "Frequency" : data.grouped.frequency, - "Midpoint" : data.grouped.midpoint, +# dfg = pd.DataFrame( +# { +# "Class Interval" : data.grouped.ranges, +# "Class Limit" : data.grouped.limit, +# "Frequency" : data.grouped.frequency, +# "Midpoint" : data.grouped.midpoint, - "C <" : data.grouped.bottom_limit, - "CF <" : data.grouped.bottom_cumulative_frequency, - "C >" : data.grouped.top_limit, - "CF >" : data.grouped.top_cumulative_frequency, - "Relative Frequency" : data.grouped.percentage_relative_frequency - } -) +# "C <" : data.grouped.bottom_limit, +# "CF <" : data.grouped.bottom_cumulative_frequency, +# "C >" : data.grouped.top_limit, +# "CF >" : data.grouped.top_cumulative_frequency, +# "Relative Frequency" : data.grouped.percentage_relative_frequency +# } +# ) # Simple Populated Data dfs = pd.DataFrame( @@ -59,15 +60,27 @@ tablefmt='pipe' ) -tablegrouped = tabulate.tabulate( - dfg, - headers='keys', - tablefmt='pipe', -) +# tablegrouped = tabulate.tabulate( +# dfg, +# headers='keys', +# tablefmt='pipe', +# ) # Print The Processed Data print(tablesimple) -print(tablegrouped) +# print(tablegrouped) + +chart = ct.Chart(title="Dataset Box Diagram", xlabel="Data", ylabel="Value") + +chart.box(data.simple.classval, data.simple.frequency) + +# Display the prepared chart +chart.show() +# Example for pie chart +data.PopulateGrouped() # Grouped Data +chart = ct.Chart(title="Grouped Frequency Pie Chart") +chart.pie(data.grouped.frequency, labels=data.grouped.ranges) +chart.show() diff --git a/FrequencyTable.py b/FrequencyTable.py index 046b16c..4910f37 100644 --- a/FrequencyTable.py +++ b/FrequencyTable.py @@ -62,13 +62,16 @@ def __init__(self, dataset): ((self.length - 1) * (self.length - 2) * (self.length - 3))) - \ (3 * (self.length - 1) ** 2) / ((self.length - 2) * (self.length - 3)) - # Method to reset the processed data - def reset_data(self): + # Base 5 Rounding + def roundy(self, x, base=5): + return base * round(x / base) + + # Function to Reset Frequency Table Data + def reset(self): global top, bottom, top_limit, bottom_limit, frequency global data_range, data_limit, data_midpoint global bot_cumulative_frequency, top_cumulative_frequency, relative_frequency, mode - - # Clear all global variables used for data processing + top.clear() bottom.clear() top_limit.clear() @@ -82,10 +85,6 @@ def reset_data(self): relative_frequency.clear() mode.clear() - # Base 5 Rounding - def roundy(self, x, base=5): - return base * round(x / base) - # Function To Find Frequency in Dataset with Desired Range (Top and Down Limit) def find_frequency(self, bot, top): total_frequency = 0 @@ -109,8 +108,7 @@ def find_frequency(self, bot, top): # Populate Grouped Table Frequency Data Method def PopulateGrouped(self): - # Reset data before populating - self.reset_data() + self.reset() # Reset the frequency table data before processing # Initiating Used Parameter for Frequency Table old_number = 0 @@ -173,20 +171,17 @@ def PopulateGrouped(self): frequency, data_range, data_limit, data_midpoint, bot_cumulative_frequency, top_cumulative_frequency, relative_frequency, mode) - + # Populate Simple Table Frequency Data Method def PopulateSimple(self): - # Reset data before populating - self.reset_data() + self.reset() # Reset the frequency table data before processing # Initialize general variables data = sorted(set(self.dataset)) # Remove duplicates and sort the data - # Check if the dataset is not entirely string-based (for numeric data) - if not all(isinstance(item, str) for item in self.dataset): - # Initialize limits for numeric data - top_limit = [] - bottom_limit = [] + # Initialize limits for numeric data + top_limit = [] + bottom_limit = [] # Single loop to process both numeric and string data for current_class in data: @@ -199,7 +194,7 @@ def PopulateSimple(self): relative_frequency.append(current_relative_frequency) # If the data is numeric, calculate limits and cumulative frequencies - if top_limit is not None and bottom_limit is not None: + if not all(isinstance(item, str) for item in self.dataset): # Calculate top and bottom limits for numeric data current_top_limit = current_class + 0.5 current_bottom_limit = current_class - 0.5 @@ -235,7 +230,7 @@ def PopulateSimple(self): bot_cumulative_frequency, top_cumulative_frequency, relative_frequency, mode ) - + # Processed Data Assignment class ProcessedData: # Limit (L), Frequency (F), Ranges (R), Midpoint (M), Cumulative (C), Relative (R) @@ -253,4 +248,4 @@ def __init__(self, data, bot, top, bot_L, top_L, F, R, L, M, bot_CF, top_CF, RF, self.top_cumulative_frequency = top_CF self.relative_frequency = RF self.percentage_relative_frequency = [f"{rf * 1:.2f}%" for rf in self.relative_frequency] - self.mode = mode \ No newline at end of file + self.mode = mode