Skip to content

Commit

Permalink
fix: Error Processing Text Data in Simple Frequency Table Because Out…
Browse files Browse the repository at this point in the history
…bound List
  • Loading branch information
brotherzhafif committed Oct 14, 2024
1 parent 968dab7 commit 239a9fd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
53 changes: 33 additions & 20 deletions Example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# EXAMPLE PROGRAM
import FrequencyTable as ft
import Chart as ct
import pandas as pd
import tabulate as tabulate

Expand All @@ -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(
Expand All @@ -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()


37 changes: 16 additions & 21 deletions FrequencyTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
self.mode = mode

0 comments on commit 239a9fd

Please sign in to comment.